-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
42 lines (31 loc) · 1.15 KB
/
Solution.java
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package cyclicrotation;
import java.util.ArrayList;
public class Solution {
public static int[] solution(int[] A, int K) {
ArrayList<Integer> previousArray = new ArrayList<Integer>();
ArrayList<Integer> currentArray;
if (A.length == 0) return A;
for (int i = 0; i < A.length; i++) {
previousArray.add(new Integer(A[i]));
}
currentArray = new ArrayList<>(previousArray);
for (int i = 0; i < K; i++) {
for (int j = 1; j < previousArray.size(); j++) {
currentArray.set(j, previousArray.get(j - 1));
}
currentArray.set(0, previousArray.get(previousArray.size() - 1));
previousArray = new ArrayList<>(currentArray);
}
int[] solution = new int[A.length];
for (int i = 0; i < A.length; i++) {
solution[i] = currentArray.get(i);
}
return solution;
}
public static void main(String[] args) {
int[] solution = solution(new int[]{1, 2, 3, 6}, 2);
for (int i = 0; i < solution.length; i++) {
System.out.println("Solution " + solution[i]);
}
}
}