-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path189.java
More file actions
45 lines (41 loc) · 1.15 KB
/
Copy path189.java
File metadata and controls
45 lines (41 loc) · 1.15 KB
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
43
44
45
public class rotateArray {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7};
int k = 3;
PrintArray(arr);
ReverseRotateArray(arr, k);
PrintArray(arr);
}
public static void PrintArray(int[] arr) {
for (int val : arr)
System.out.print(val + " ");
System.out.println();
}
public static void BruteForceRotateArray(int[] nums, int k) {
int n = nums.length;
k %= n;
for (int i=0; i<k; i++) {
int lastElement = nums[n-1];
for (int j=n-1; j>0; j--) {
nums[j] = nums[j-1];
}
nums[0] = lastElement;
}
}
public static void ReverseRotateArray(int[] nums, int k) {
int n = nums.length;
k %= n;
reverse(nums, 0, n-1);
reverse(nums, 0, k-1);
reverse(nums, k, n-1);
}
public static void reverse(int[] nums, int start, int end) {
while (start < end) {
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}
}