1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| public class CyclicRotation_lesson02 { public static void main(String[] args) { solution(new int[]{3, 8, 9, 7, 6},3); } static int[] solution(int[] A, int K) { LinkedList<Integer> linkedList = new LinkedList<>(); if(A.length == 0){ return A; } for(int i=0; i<A.length; i++){ linkedList.add(A[i]); } for(int i=0; i<K; i++){ int last = linkedList.removeLast(); linkedList.addFirst(last); } int[] answer = new int[linkedList.size()]; int idx = 0; for (Integer integer : linkedList) { answer[idx++] = integer; } return answer; } }
|