Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
39 lines (36 loc) · 1.14 KB

File metadata and controls

39 lines (36 loc) · 1.14 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
public class Solution {
public void rotateWithCopy(int[] nums, int k) {
int[] results = new int[nums.length];
k = k % nums.length;
System.arraycopy(nums, 0, results, k, nums.length-k);
System.arraycopy(nums, nums.length-k, results, 0, k);
System.arraycopy(results, 0, nums, 0, nums.length);
}
public void rotateWithKTimes(int[] nums, int k) {
k = k % nums.length;
int count = 0;
while (count < k) {
int temp = nums[nums.length-1];
for (int i = nums.length-1; i > 0; i --) {
nums[i] = nums[i-1];
}
nums[0] = temp;
count += 1;
}
}
public void rotate(int[] nums, int k) {
k = k % nums.length;
this.reverse(nums, 0, nums.length-1);
this.reverse(nums, 0, k-1);
this.reverse(nums, k, nums.length-1);
}
public void reverse (int[] nums, int left, int right) {
while (left < right) {
int temp = nums[left];
nums[left] = nums[right];
nums[right] = temp;
left += 1;
right -= 1;
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.