Skip to content

Commit 4323cc8

Browse files
refactor 26
1 parent 3eb7c15 commit 4323cc8

File tree

2 files changed

+96
-42
lines changed

2 files changed

+96
-42
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,58 @@
11
package com.fishercoder.solutions;
22

3-
/** Given a sorted array, remove the duplicates
3+
/**
4+
* 26. Remove Duplicates from Sorted Array
5+
*
6+
* Given a sorted array, remove the duplicates
47
* in place such that each element appear only once and return the new length.
58
* Do not allocate extra space for another array, you must do this in place with constant memory.
6-
7-
For example,
8-
Given input array A = [1,1,2],
9-
10-
Your function should return length = 2, and A is now [1,2].*/
9+
*
10+
* For example,
11+
* Given input array A = [1,1,2],
12+
* Your function should return length = 2, and A is now [1,2].
13+
* */
1114

1215
public class _26 {
1316

14-
public static int removeDuplicates_editorial_solution(int[] nums) {
15-
int i = 0;
16-
for (int j = 1; j < nums.length; j++) {
17-
if (nums[i] != nums[j]) {
18-
i++;
19-
nums[i] = nums[j];
20-
}
17+
public static class Solution1 {
18+
public int removeDuplicates(int[] nums) {
19+
int i = 0;
20+
for (int j = 1; j < nums.length; j++) {
21+
if (nums[i] != nums[j]) {
22+
i++;
23+
nums[i] = nums[j];
2124
}
22-
return i + 1;
23-
}
24-
25-
26-
public static void main(String... strings) {
27-
int[] nums = new int[]{1, 1, 2};
28-
// int[] nums = new int[]{1,1,2,2,3};
29-
// int[] nums = new int[]{1,1};
30-
System.out.println(removeDuplicates_editorial_solution(nums));
25+
}
26+
return i + 1;
3127
}
28+
}
3229

30+
public static class Solution2 {
3331
/**
3432
* Same idea as the editorial solution, mine just got more verbose.
3533
*/
36-
public static int removeDuplicates_my_original(int[] nums) {
37-
int i = 0;
38-
for (int j = i + 1; i < nums.length && j < nums.length; ) {
39-
while (j < nums.length && nums[i] == nums[j]) {
40-
j++;
41-
}
42-
if (j == nums.length) {
43-
j--;
44-
}
45-
int temp = nums[j];
46-
nums[j] = nums[i + 1];
47-
nums[i + 1] = temp;
48-
if (nums[i] != nums[i + 1]) {
49-
i++;
50-
}
51-
if (j == nums.length) {
52-
break;
53-
}
54-
j++;
34+
public static int removeDuplicates(int[] nums) {
35+
int i = 0;
36+
for (int j = i + 1; i < nums.length && j < nums.length; ) {
37+
while (j < nums.length && nums[i] == nums[j]) {
38+
j++;
39+
}
40+
if (j == nums.length) {
41+
j--;
42+
}
43+
int temp = nums[j];
44+
nums[j] = nums[i + 1];
45+
nums[i + 1] = temp;
46+
if (nums[i] != nums[i + 1]) {
47+
i++;
48+
}
49+
if (j == nums.length) {
50+
break;
5551
}
56-
return i + 1;
52+
j++;
53+
}
54+
return i + 1;
5755
}
56+
}
5857

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

0 commit comments

Comments
 (0)