|
3 | 3 | import java.util.ArrayList;
|
4 | 4 | import java.util.List;
|
5 | 5 |
|
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 | + * */ |
7 | 14 |
|
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].*/ |
9 | 15 | public class _189 {
|
10 | 16 |
|
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 | + } |
19 | 27 | }
|
20 | 28 | }
|
21 | 29 |
|
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; |
39 | 38 | }
|
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; |
44 | 41 | }
|
| 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 | + } |
45 | 54 |
|
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 | + } |
52 | 62 | }
|
53 | 63 | }
|
54 | 64 |
|
|
0 commit comments