-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotateArray.java
More file actions
28 lines (26 loc) · 863 Bytes
/
Copy pathRotateArray.java
File metadata and controls
28 lines (26 loc) · 863 Bytes
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
import java.util.Arrays;
class RotateArray {
public void rotate(int[] nums, int k) {
if (nums.length < k) {
k = k % nums.length;
}
int[] suffix = new int[k];
for (int m = 0; m < k; m++) {
suffix[k - m - 1] = nums[nums.length - m - 1];
}
for (int i = nums.length - 1; i >= k; i--) {
nums[i] = nums[i - k];
}
for (int m = 0; m < k; m++) {
nums[m] = suffix[m];
}
}
public static void main(String[] args) {
RotateArray solution = new RotateArray();
int[] nums = {1, 2, 3, 4, 5, 6, 7};
int k = 3;
System.out.println("Before rotation: " + Arrays.toString(nums));
solution.rotate(nums, k);
System.out.println("After rotation: " + Arrays.toString(nums));
}
}