-
Notifications
You must be signed in to change notification settings - Fork 1
/
RotateArray.java
63 lines (58 loc) · 2.01 KB
/
RotateArray.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//Given an array, rotate the array to the right by k steps, where k is non-negative.
public class RotateArray {
public static void main(String[] args) {
int k = 9;
int[] nums = {1,2,3,4,5,6,7,8,9,10};
rotate(nums,k);
}
//k is the number of steps to rotate the array to the right
// public static void rotate(int[] nums, int k) {
// if(nums.length <= 1){
// return;
// }
// //step each time to move
// int step = k % nums.length;
// int[] tmp = new int[step];
// //tmp grabs the numbers that are going past the array length and back into the beginning of the array
// for(int i = 0; i < step; i++){
// tmp[i] = nums[nums.length - step + i];
// }
// for(int i = nums.length - 1 - step; i >= 0; i--){
// nums[i + step] = nums[i];
// }
// for(int i = 0; i < step; i++){
// nums[i] = tmp[i];
// }
//
//
// }
public static void rotate(int[] nums, int k) {
k %= nums.length;
reverse(nums, 0, nums.length - 1); //we have to first reverse the whole array by swapping first element with the last one and so on..
reverse(nums, 0, k - 1); //reverse the elements from 0 to k-1
reverse(nums, k, nums.length - 1); //reverse the elements from k to 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--;
}
}
}
//SOLUTION:
//let a= [1,2,3,4,5,6,7]
// k = 3.
//
// we have to first reverse the whole array by swapping first element with the last one and so on..
// you will get[7,6,5,4,3,2,1]
//
// reverse the elements from 0 to k-1
// reverse the elements 7,6,5
// you will get [5,6,7,4,3,2,1]
//
// reverse the elements from k to n-1
// reverse the elements 4,3,2,1
// you will get[5,6,7,1,2,3,4]