-
CyclicRotation알고리즘/Codility 2020. 9. 13. 21:24
app.codility.com/demo/results/training8K9DTZ-447/
Test results - Codility
An 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 moved to the first place. For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9,
app.codility.com
문제) 배열 A의 값을 K만큼 우측으로 이동시켜 출력. 마지막 배열의 값은 배열의 첫번째로 다시 이동.
나의 해답
public class CyclicRotation { public int[] solution(int[] A, int K) { int[] answer = new int[A.length]; for(int idx=0; idx < A.length; idx++) { int tempIdx = idx + K; while(tempIdx >= A.length) { tempIdx -= A.length; } answer[tempIdx] = A[idx]; } return answer; } /** * k만큼 우측으로 움직이기. * */ public static void main(String[] args) { //int[] a = {3, 8, 9, 7, 6}; //int k = 3; int[] a= {1, 2, 3, 4}; int k = 10; int[] answer = new CyclicRotation().solution(a, k); for(int ans : answer) { System.out.print(ans); } } }
'알고리즘 > Codility' 카테고리의 다른 글
MissingInteger (0) 2020.09.13 BinaryGap (0) 2020.09.13