Lesson2 - CyclicRotation

作成日:2018/2/13
CyclicRotation coding task - Learn to Code - Codility

// 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 static int[] solution(int[] A, int K) {

      int length = A.length;
      int[] replace = new int[length];
      int cc = 0;

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

        if( length > i + K ) {
          replace[i+K] = A[i];
        } else if ( length > K ) {
          replace[(i+K) - (length)] = A[i];
        } else {
          cc = (i+K) - (length);
          while ( cc >= length ) {
            cc = cc - length;
          }
          replace[cc] = A[i];
        }

      }

      return replace;
    }
}