프로그래밍/알고리즘

[Codility] cyclicRotation

낭만가을 2018. 4. 15. 21:26

For example, given

A = [3, 8, 9, 7, 6] K = 3

the function should return [9, 7, 6, 3, 8]. Three rotations were made:

[3, 8, 9, 7, 6] -> [6, 3, 8, 9, 7] [6, 3, 8, 9, 7] -> [7, 6, 3, 8, 9] [7, 6, 3, 8, 9] -> [9, 7, 6, 3, 8]

For another example, given

A = [0, 0, 0] K = 1

the function should return [0, 0, 0]

Given

A = [1, 2, 3, 4] K = 4

the function should return [1, 2, 3, 4]


설명:

A 의 배열을 K 만큼 우측 으로 이동했을시 나오는 A를 반환하면 된다.




class Solution { public int[] solution(int[] A, int K) { // write your code in Java SE 8 int[] rotaionA = new int[A.length]; for(int i =0;i< A.length;i++){ rotaionA[ (i+K) % A.length ] = A[i]; } return rotaionA; } }