Skip to content

Commit f77b9bb

Browse files
refactor 189
1 parent 43e30bb commit f77b9bb

File tree

2 files changed

+87
-68
lines changed

2 files changed

+87
-68
lines changed

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

Lines changed: 63 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -3,83 +3,78 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6-
import static com.fishercoder.solutions._189.Solution2.rotate_naive;
7-
86
/**
97
* 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-
* */
14-
15-
public class _189 {
16-
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-
}
27-
}
28-
}
298
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;
38-
}
39-
if (k > nums.length) {
40-
k -= nums.length;
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-
}
9+
Given an array, rotate the array to the right by k steps, where k is non-negative.
5410
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-
}
62-
}
63-
}
11+
Example 1:
12+
Input: [1,2,3,4,5,6,7] and k = 3
13+
Output: [5,6,7,1,2,3,4]
14+
Explanation:
15+
rotate 1 steps to the right: [7,1,2,3,4,5,6]
16+
rotate 2 steps to the right: [6,7,1,2,3,4,5]
17+
rotate 3 steps to the right: [5,6,7,1,2,3,4]
6418
65-
public static void main(String... strings) {
66-
// int k = 1;
67-
// int[] nums = new int[]{1,2,3};
68-
// int[] nums = new int[]{1};
69-
// int[] nums = new int[]{1,2};
19+
Example 2:
20+
Input: [-1,-100,3,99] and k = 2
21+
Output: [3,99,-1,-100]
22+
Explanation:
23+
rotate 1 steps to the right: [99,-1,-100,3]
24+
rotate 2 steps to the right: [3,99,-1,-100]
7025
71-
// int k = 3;
72-
// int[] nums = new int[]{1,2};
26+
Note:
27+
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
28+
Could you do it in-place with O(1) extra space?
7329
74-
// int k = 2;
75-
// int[] nums = new int[]{1,2};
30+
* */
7631

77-
int k = 4;
78-
int[] nums = new int[]{1, 2, 3};
32+
public class _189 {
7933

80-
// int k = 2;
81-
// int[] nums = new int[]{-1};
82-
rotate_naive(nums, k);
34+
public static class Solution1 {
35+
public void rotate(int[] nums, int k) {
36+
int len = nums.length;
37+
int[] tmp = new int[len];
38+
for (int i = 0; i < len; i++) {
39+
tmp[(i + k) % len] = nums[i];
40+
}
41+
for (int i = 0; i < len; i++) {
42+
nums[i] = tmp[i];
43+
}
8344
}
45+
}
8446

47+
public static class Solution2 {
48+
/**
49+
* 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
51+
*/
52+
public static void rotate_naive(int[] nums, int k) {
53+
if (k == 0 || k == nums.length) {
54+
return;
55+
}
56+
if (k > nums.length) {
57+
k -= nums.length;
58+
}
59+
List<Integer> tmp = new ArrayList();
60+
int i = 0;
61+
if (nums.length - k >= 0) {
62+
i = nums.length - k;
63+
for (; i < nums.length; i++) {
64+
tmp.add(nums[i]);
65+
}
66+
} else {
67+
i = nums.length - 1;
68+
for (; i >= 0; i--) {
69+
tmp.add(nums[i]);
70+
}
71+
}
72+
for (i = 0; i < nums.length - k; i++) {
73+
tmp.add(nums[i]);
74+
}
75+
for (i = 0; i < tmp.size(); i++) {
76+
nums[i] = tmp.get(i);
77+
}
78+
}
79+
}
8580
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions._189;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
public class _189Test {
9+
private static _189.Solution1 solution1;
10+
private static int[] nums;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _189.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
nums = new int[] {1, 2, 3};
20+
21+
solution1.rotate(nums, 1);
22+
CommonUtils.printArray(nums);
23+
}
24+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy