Skip to content

Commit 267d3c1

Browse files
[N-0] refactor 189
1 parent 185d879 commit 267d3c1

File tree

1 file changed

+47
-37
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+47
-37
lines changed

src/main/java/com/fishercoder/solutions/_189.java

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,62 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6-
/**Rotate an array of n elements to the right by k steps.
6+
import static com.fishercoder.solutions._189.Solution2.rotate_naive;
7+
8+
/**
9+
* 189. Rotate Array
10+
*
11+
* Rotate an array of n elements to the right by k steps.
12+
* For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].
13+
* */
714

8-
For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].*/
915
public class _189 {
1016

11-
public void rotate(int[] nums, int k) {
12-
int len = nums.length;
13-
int[] tmp = new int[len];
14-
for (int i = 0; i < len; i++) {
15-
tmp[(i + k) % len] = nums[i];
16-
}
17-
for (int i = 0; i < len; i++) {
18-
nums[i] = tmp[i];
17+
public static class Solution1 {
18+
public void rotate(int[] nums, int k) {
19+
int len = nums.length;
20+
int[] tmp = new int[len];
21+
for (int i = 0; i < len; i++) {
22+
tmp[(i + k) % len] = nums[i];
23+
}
24+
for (int i = 0; i < len; i++) {
25+
nums[i] = tmp[i];
26+
}
1927
}
2028
}
2129

22-
/**
23-
* My original idea and got AC'ed.
24-
* One thing to notice is that when k > nums.length, we'll continue to rotate_naive the array, it just becomes k -= nums.length
25-
*/
26-
public static void rotate_naive(int[] nums, int k) {
27-
if (k == 0 || k == nums.length) {
28-
return;
29-
}
30-
if (k > nums.length) {
31-
k -= nums.length;
32-
}
33-
List<Integer> tmp = new ArrayList();
34-
int i = 0;
35-
if (nums.length - k >= 0) {
36-
i = nums.length - k;
37-
for (; i < nums.length; i++) {
38-
tmp.add(nums[i]);
30+
public static class Solution2 {
31+
/**
32+
* My original idea and got AC'ed.
33+
* One thing to notice is that when k > nums.length, we'll continue to rotate_naive the array, it just becomes k -= nums.length
34+
*/
35+
public static void rotate_naive(int[] nums, int k) {
36+
if (k == 0 || k == nums.length) {
37+
return;
3938
}
40-
} else {
41-
i = nums.length - 1;
42-
for (; i >= 0; i--) {
43-
tmp.add(nums[i]);
39+
if (k > nums.length) {
40+
k -= nums.length;
4441
}
42+
List<Integer> tmp = new ArrayList();
43+
int i = 0;
44+
if (nums.length - k >= 0) {
45+
i = nums.length - k;
46+
for (; i < nums.length; i++) {
47+
tmp.add(nums[i]);
48+
}
49+
} else {
50+
i = nums.length - 1;
51+
for (; i >= 0; i--) {
52+
tmp.add(nums[i]);
53+
}
4554

46-
}
47-
for (i = 0; i < nums.length - k; i++) {
48-
tmp.add(nums[i]);
49-
}
50-
for (i = 0; i < tmp.size(); i++) {
51-
nums[i] = tmp.get(i);
55+
}
56+
for (i = 0; i < nums.length - k; i++) {
57+
tmp.add(nums[i]);
58+
}
59+
for (i = 0; i < tmp.size(); i++) {
60+
nums[i] = tmp.get(i);
61+
}
5262
}
5363
}
5464

0 commit comments

Comments
 (0)