forked from lgaBug/algorithm014-algorithm014
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.java
More file actions
35 lines (31 loc) · 840 Bytes
/
main.java
File metadata and controls
35 lines (31 loc) · 840 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
34
35
package Week_01.Rotate_array_189;
import java.util.Arrays;
public class main {
public static void main(String[] args) {
int[] nums = new int[]{1,2,3,4,5,6,7};
Solution s = new Solution();
s.rotate(nums, 4);
System.out.println(Arrays.toString(nums));
}
}
class Solution {
public void rotate(int[] nums, int k) {
if ( nums.length <= 0 ) {
return;
}
int n = nums.length;
k = k % n;
this.reverse(nums, 0, n - 1);
this.reverse(nums, 0, k - 1);
this.reverse(nums, k, n - 1);
}
public void reverse(int [] nums, int start, int end) {
while ( start < end ) {
int tmp = nums[end];
nums[end] = nums[start];
nums[start] = tmp;
start++;
end--;
}
}
}