-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path189.java
More file actions
42 lines (37 loc) · 1.19 KB
/
189.java
File metadata and controls
42 lines (37 loc) · 1.19 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
__________________________________________________________________________________________________
sample 0 ms submission
class Solution {
public void rotate(int[] nums, int k) {
int len = nums.length;
k %= len;
if(k == 0) return;
int[] t = nums.clone();
for(int i = 0; i < nums.length; i++){
int j = i;
if(j - k < 0) j += len;
// System.out.println(t[j-k]);
nums[i] = t[j-k];
}
return;
}
}
__________________________________________________________________________________________________
sample 34544 kb submission
class Solution {
public void rotate(int[] nums, int k) {
k = k % nums.length;
this.help(nums, 0, nums.length-k-1);
this.help(nums, nums.length-k, nums.length-1);
this.help(nums, 0, nums.length-1);
}
private void help(int[] nums, int h, int t){
while(h < t){
nums[h] ^= nums[t];
nums[t] ^= nums[h];
nums[h] ^= nums[t];
h++;
t--;
}
}
}
__________________________________________________________________________________________________