Skip to content

Commit a8deb5e

Browse files
refactor 75
1 parent fa88687 commit a8deb5e

File tree

2 files changed

+74
-24
lines changed

2 files changed

+74
-24
lines changed

Diff for: src/main/java/com/fishercoder/solutions/_75.java

+22-24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.fishercoder.solutions;
22

3-
/** Given an array with n objects colored red, white or blue,
3+
/**
4+
* 75. Sort Colors
5+
*
6+
* Given an array with n objects colored red, white or blue,
47
* sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.
58
69
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
@@ -9,39 +12,34 @@
912
You are not suppose to use the library's sort function for this problem.
1013
1114
Follow up:
15+
1216
A rather straight forward solution is a two-pass algorithm using counting sort.
1317
First, iterate the array counting number of 0's, 1's, and 2's,
1418
then overwrite array with total number of 0's, then 1's and followed by 2's.
19+
Could you come up with an one-pass algorithm using only constant space?
20+
*/
1521

16-
Could you come up with an one-pass algorithm using only constant space?*/
1722
public class _75 {
1823

24+
public static class Solution1 {
1925
public void sortColors(int[] nums) {
20-
int zero = 0;
21-
int two = nums.length - 1;
22-
for (int i = 0; i <= two; ) {
23-
if (nums[i] == 0 && i > zero) {
24-
swap(nums, i, zero++);
25-
} else if (nums[i] == 2 && i < two) {
26-
swap(nums, i, two--);
27-
} else {
28-
i++;
29-
}
26+
int zero = 0;
27+
int two = nums.length - 1;
28+
for (int i = 0; i <= two; ) {
29+
if (nums[i] == 0 && i > zero) {
30+
swap(nums, i, zero++);
31+
} else if (nums[i] == 2 && i < two) {
32+
swap(nums, i, two--);
33+
} else {
34+
i++;
3035
}
36+
}
3137
}
3238

3339
void swap(int[] nums, int m, int n) {
34-
int temp = nums[m];
35-
nums[m] = nums[n];
36-
nums[n] = temp;
37-
}
38-
39-
40-
public static void main(String... args) {
41-
// int[] nums = new int[]{0,1,2,0,2,1};
42-
// int[] nums = new int[]{0};
43-
// int[] nums = new int[]{2};
44-
int[] nums = new int[]{2, 2, 1};
45-
// int[] nums = new int[]{1,0};
40+
int temp = nums[m];
41+
nums[m] = nums[n];
42+
nums[n] = temp;
4643
}
44+
}
4745
}

Diff for: src/test/java/com/fishercoder/_75Test.java

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._75;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _75Test {
10+
private static _75.Solution1 solution1;
11+
private static int[] nums;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _75.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
nums = new int[] {2, 2, 1};
21+
solution1.sortColors(nums);
22+
assertArrayEquals(new int[] {1, 2, 2}, nums);
23+
}
24+
25+
@Test
26+
public void test2() {
27+
nums = new int[] {0, 1, 2, 0, 2, 1};
28+
solution1.sortColors(nums);
29+
assertArrayEquals(new int[] {0, 0, 1, 1, 2, 2}, nums);
30+
}
31+
32+
@Test
33+
public void test3() {
34+
nums = new int[] {0};
35+
solution1.sortColors(nums);
36+
assertArrayEquals(new int[] {0}, nums);
37+
}
38+
39+
@Test
40+
public void test4() {
41+
nums = new int[] {1, 0};
42+
solution1.sortColors(nums);
43+
assertArrayEquals(new int[] {0, 1}, nums);
44+
}
45+
46+
@Test
47+
public void test5() {
48+
nums = new int[] {2};
49+
solution1.sortColors(nums);
50+
assertArrayEquals(new int[] {2}, nums);
51+
}
52+
}

0 commit comments

Comments
 (0)