Skip to content

Commit 796ecc2

Browse files
refactor 189
1 parent fe63f53 commit 796ecc2

File tree

1 file changed

+4
-40
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+4
-40
lines changed
Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package com.fishercoder.solutions;
22

3-
import java.util.ArrayList;
4-
import java.util.List;
5-
63
/**
74
* 189. Rotate Array
85
@@ -33,9 +30,10 @@ public class _189 {
3330

3431
public static class Solution1 {
3532
/**
33+
* using an extra array of the same size to copy it
3634
* O(n) space
3735
* O(n) time
38-
* */
36+
*/
3937
public void rotate(int[] nums, int k) {
4038
int len = nums.length;
4139
int[] tmp = new int[len];
@@ -52,7 +50,7 @@ public static class Solution2 {
5250
/**
5351
* O(1) space
5452
* O(n) time
55-
* */
53+
*/
5654
public void rotate(int[] nums, int k) {
5755
int tmp;
5856
for (int i = 0; i < k; i++) {
@@ -64,38 +62,4 @@ public void rotate(int[] nums, int k) {
6462
}
6563
}
6664
}
67-
68-
public static class Solution3 {
69-
/**
70-
* My original idea and got AC'ed.
71-
* One thing to notice is that when k > nums.length, we'll continue to rotate the array, it just becomes k -= nums.length
72-
*/
73-
public static void rotate(int[] nums, int k) {
74-
if (k == 0 || k == nums.length) {
75-
return;
76-
}
77-
if (k > nums.length) {
78-
k -= nums.length;
79-
}
80-
List<Integer> tmp = new ArrayList();
81-
int i = 0;
82-
if (nums.length - k >= 0) {
83-
i = nums.length - k;
84-
for (; i < nums.length; i++) {
85-
tmp.add(nums[i]);
86-
}
87-
} else {
88-
i = nums.length - 1;
89-
for (; i >= 0; i--) {
90-
tmp.add(nums[i]);
91-
}
92-
}
93-
for (i = 0; i < nums.length - k; i++) {
94-
tmp.add(nums[i]);
95-
}
96-
for (i = 0; i < tmp.size(); i++) {
97-
nums[i] = tmp.get(i);
98-
}
99-
}
100-
}
101-
}
65+
}

0 commit comments

Comments
 (0)