Skip to content

Commit fe63f53

Browse files
refactor 26
1 parent 9bc8e4f commit fe63f53

File tree

2 files changed

+50
-80
lines changed

2 files changed

+50
-80
lines changed

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

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,37 @@
33
/**
44
* 26. Remove Duplicates from Sorted Array
55
*
6-
* Given a sorted array, remove the duplicates
7-
* in place such that each element appear only once and return the new length.
8-
* Do not allocate extra space for another array, you must do this in place with constant memory.
6+
* Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.
7+
* Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
98
*
10-
* For example,
11-
* Given input array A = [1,1,2],
12-
* Your function should return length = 2, and A is now [1,2].
9+
* Example 1:
10+
* Given nums = [1,1,2],
11+
* Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
12+
* It doesn't matter what you leave beyond the returned length.
13+
*
14+
* Example 2:
15+
* Given nums = [0,0,1,1,1,2,2,3,3,4],
16+
* Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.
17+
* It doesn't matter what values are set beyond the returned length.
18+
*
19+
* Clarification:
20+
* Confused why the returned value is an integer but your answer is an array?
21+
* Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.
22+
* Internally you can think of this:
23+
* // nums is passed in by reference. (i.e., without making a copy)
24+
* int len = removeDuplicates(nums);
25+
*
26+
* // any modification to nums in your function would be known by the caller.
27+
* // using the length returned by your function, it prints the first len elements.
28+
* for (int i = 0; i < len; i++) {
29+
* print(nums[i]);
30+
* }
1331
* */
1432

1533
public class _26 {
1634

1735
public static class Solution1 {
36+
/**Key: It doesn't matter what you leave beyond the returned length.*/
1837
public int removeDuplicates(int[] nums) {
1938
int i = 0;
2039
for (int j = 1; j < nums.length; j++) {
@@ -26,33 +45,4 @@ public int removeDuplicates(int[] nums) {
2645
return i + 1;
2746
}
2847
}
29-
30-
public static class Solution2 {
31-
/**
32-
* Same idea as the editorial solution, mine just got more verbose.
33-
*/
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;
51-
}
52-
j++;
53-
}
54-
return i + 1;
55-
}
56-
}
57-
5848
}

src/test/java/com/fishercoder/_26Test.java

Lines changed: 25 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,29 @@
77
import static org.junit.Assert.assertEquals;
88

99
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-
}
10+
private static _26.Solution1 solution1;
11+
private static int[] nums;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _26.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
nums = new int[]{1, 1, 2};
21+
assertEquals(2, solution1.removeDuplicates(nums));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
nums = new int[]{1, 1, 2, 2, 3};
27+
assertEquals(3, solution1.removeDuplicates(nums));
28+
}
29+
30+
@Test
31+
public void test3() {
32+
nums = new int[]{1, 1};
33+
assertEquals(1, solution1.removeDuplicates(nums));
34+
}
5535
}

0 commit comments

Comments
 (0)