-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path189. Rotate Array.java
More file actions
33 lines (31 loc) · 875 Bytes
/
189. Rotate Array.java
File metadata and controls
33 lines (31 loc) · 875 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
29
30
31
32
33
// class Solution {
// public void rotate(int[] nums, int k) {
// int n = nums.length;
// k = k % n;
// reverse(nums, 0, n - 1);
// reverse(nums, 0, k - 1);
// reverse(nums, k, n - 1);
// }
// private void reverse(int[] nums, int start, int end) {
// while (start < end) {
// int temp = nums[start];
// nums[start] = nums[end];
// nums[end] = temp;
// start++;
// end--;
// }
// }
// }
class Solution {
public void rotate(int[] nums, int k) {
k = k % nums.length;
while(k > 0){
int ele = nums[nums.length - 1];
for(int i = nums.length - 1; i > 0; i--){
nums[i] = nums[i - 1];
}
nums[0] = ele;
k--;
}
}
}