Skip to content

Commit 136b2ab

Browse files
refactor 189
1 parent 55f7c26 commit 136b2ab

File tree

2 files changed

+54
-27
lines changed

2 files changed

+54
-27
lines changed

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

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
public class _189 {
3333

3434
public static class Solution1 {
35+
/**
36+
* O(n) space
37+
* O(n) time
38+
* */
3539
public void rotate(int[] nums, int k) {
3640
int len = nums.length;
3741
int[] tmp = new int[len];
@@ -45,9 +49,26 @@ public void rotate(int[] nums, int k) {
4549
}
4650

4751
public static class Solution2 {
52+
/**
53+
* O(1) space
54+
* O(n) time
55+
* */
56+
public void rotate(int[] nums, int k) {
57+
int tmp;
58+
for (int i = 0; i < k; i++) {
59+
tmp = nums[nums.length - 1];
60+
for (int j = nums.length - 1; j > 0; j--) {
61+
nums[j] = nums[j - 1];
62+
}
63+
nums[0] = tmp;
64+
}
65+
}
66+
}
67+
68+
public static class Solution3 {
4869
/**
4970
* My original idea and got AC'ed.
50-
* One thing to notice is that when k > nums.length, we'll continue to rotate_naive the array, it just becomes k -= nums.length
71+
* One thing to notice is that when k > nums.length, we'll continue to rotate the array, it just becomes k -= nums.length
5172
*/
5273
public static void rotate(int[] nums, int k) {
5374
if (k == 0 || k == nums.length) {
@@ -77,18 +98,4 @@ public static void rotate(int[] nums, int k) {
7798
}
7899
}
79100
}
80-
81-
public static class Solution3 {
82-
public void rotate(int[] nums, int k) {
83-
int tmp;
84-
for (int i = 0; i < k; i++) {
85-
tmp = nums[nums.length - 1];
86-
for (int j = nums.length - 1; j > 0; j--) {
87-
nums[j] = nums[j - 1];
88-
}
89-
nums[0] = tmp;
90-
}
91-
}
92-
}
93-
94101
}

src/test/java/com/fishercoder/_189Test.java

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,39 @@
66
import org.junit.Test;
77

88
public class _189Test {
9-
private static _189.Solution1 solution1;
10-
private static int[] nums;
9+
private static _189.Solution1 solution1;
10+
private static _189.Solution2 solution2;
11+
private static _189.Solution3 solution3;
12+
private static int[] nums;
1113

12-
@BeforeClass
13-
public static void setup() {
14-
solution1 = new _189.Solution1();
15-
}
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _189.Solution1();
17+
solution2 = new _189.Solution2();
18+
solution3 = new _189.Solution3();
19+
}
1620

17-
@Test
18-
public void test1() {
19-
nums = new int[] {1, 2, 3};
21+
@Test
22+
public void test1() {
23+
nums = new int[]{1, 2, 3};
2024

21-
solution1.rotate(nums, 1);
22-
CommonUtils.printArray(nums);
23-
}
25+
solution1.rotate(nums, 1);
26+
CommonUtils.printArray(nums);
27+
}
28+
29+
@Test
30+
public void test2() {
31+
nums = new int[]{1, 2, 3};
32+
33+
solution2.rotate(nums, 1);
34+
CommonUtils.printArray(nums);
35+
}
36+
37+
@Test
38+
public void test3() {
39+
nums = new int[]{1, 2, 3};
40+
41+
solution3.rotate(nums, 1);
42+
CommonUtils.printArray(nums);
43+
}
2444
}

0 commit comments

Comments
 (0)