A zero-indexed array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is also moved to the first place.

For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7]. The goal is to rotate array A K times; that is, each element of A will be shifted to the right by K indexes.

Write a function:

class Solution { public int[] solution(int[] A, int K); }

that, given a zero-indexed array A consisting of N integers and an integer K, returns the array A rotated K times.

For example, given array A = [3, 8, 9, 7, 6] and K = 3, the function should return [9, 7, 6, 3, 8].

Assume that:

  • N and K are integers within the range [0..100];
  • each element of array A is an integer within the range [−1,000..1,000].

In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.

Copyright 2009–2016 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.

 

출처: <https://codility.com/c/run/trainingSMPM3W-Y7E>

 

 

// you can also use imports, for example:

// import java.util.*;

 

// you can write to stdout for debugging purposes, e.g.

// System.out.println("this is a debug message");

 

class Solution {

   

    static class MyNode {

        int info;

       

        MyNode next;

       

        public MyNode(int p) {

            info = p;

        }     

    }

   

    public MyNode mHead;   

   

    public Solution() { this.mHead = null; }

   

    public void add(MyNode pNode) {

        if(mHead ==null) {

            mHead = pNode;

        } else {

            MyNode n = mHead;

            while(n.next != null) n = n.next;

            n.next = pNode;

        }

    }

       

    public Solution makeCircleQueue(Solution list) {     

        MyNode n = list.mHead;

        while(n.next != null) n = n.next;

        n.next = list.mHead;

        return list;

    }

   

        public int[] solution(int[] A, int K) {

        // write your code in Java SE 8

        Solution list = new Solution();

        for(int i=0; i< A.length ; i++) {

            list.add(new MyNode(A[i]));

        }

       

        list = makeCircleQueue(list);

       

        int[] RETN = new int[A.length];

       

        MyNode temp = list.mHead;

       

        for(int i=1; i<K; i++) temp = temp.next;

               

        for(int i=0; i < A.length ; i++) {           

            RETN[i] = temp.info;         

            temp = temp.next; //ERROR

        }

   

        return RETN;

    }

}

 

// you can also use imports, for example:

// import java.util.*;

 

// you can write to stdout for debugging purposes, e.g.

// System.out.println("this is a debug message");

 

class Solution {

    public int[] solution(int[] A, int K) {

        // write your code in Java SE 8

        int[] B = new int[A.length];

        int reIndex;

        for(int i=0; i<A.length ; i++) {

            reIndex = (i+K) % A.length;

            B[reIndex] = A[i];

        }

       

        return B;

    }

}

 

// ConsoleApplication1.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.
//

#include 
#include 
#include 

typedef struct _mySTRUCT
{
char name[30];
int age;
} mySTRUCT;

void funcA(mySTRUCT*);
void funcB(mySTRUCT*);
void funcC(mySTRUCT*);

int main()
{
mySTRUCT a = (mySTRUCT)malloc(sizeof(mySTRUCT));

void(*func[3])(mySTRUCT*) = { funcA, funcB, funcC };

memset(a, 0, sizeof(mySTRUCT));

strcpy_s(a->name, "hajunho");
a->age = 37;

printf("MAIN : %s, %d\n", a->name, a->age);

for (int i = 0; i < 3; i++) func[i](a);

return 0;
}

void funcA(mySTRUCT *b)
{
printf("funcA : %s, %d\n", b->name, b->age);
strcpy_s(b->name, "leesora");
b->age = 33;
}

void funcB(mySTRUCT *c)
{
printf("funcB : %s, %d\n", c->name, c->age);
strcpy_s(c->name, "hasoyul");
c->age = 6;
}

void funcC(mySTRUCT *d)
{
printf("funcC : %s, %d\n", d->name, d->age);
}

'Blog History' 카테고리의 다른 글

178  (0) 2020.04.14
177  (0) 2020.04.14
175  (0) 2020.04.14
174  (0) 2020.04.14
173  (0) 2020.04.14

+ Recent posts