Algorithm/Problem Solving

[Java] Codility - CyclicRotation 문제 풀이

JeongKyun 2022. 9. 5.

문제

문제 바로가기

 


 

Task Score

100%

 

Correctness

100%

 

Performance

Not assessed

 


 

문제 풀이

// 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) {
        for (int i = 0; i < K; i++) {
            boolean isFirst = true;
            int[] newArr = new int[A.length];
            for (int j = 0; j < A.length; j++) {
                if (isFirst) {
                    newArr[j] = A[newArr.length - 1];
                    isFirst = false;
                } else {
                    newArr[j] = A[j - 1];
                }
            }
            A = newArr;
        }

        return A;
    }
}
반응형

댓글

💲 많이 본 글