File tree 2 files changed +39
-11
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder
2 files changed +39
-11
lines changed Original file line number Diff line number Diff line change 1
1
package com .fishercoder .solutions ;
2
2
3
+ import com .fishercoder .common .utils .CommonUtils ;
4
+
3
5
public class _26 {
4
6
5
- public static class Solution1 {
6
- /**Key: It doesn't matter what you leave beyond the returned length.*/
7
- public int removeDuplicates (int [] nums ) {
8
- int i = 0 ;
9
- for (int j = 1 ; j < nums .length ; j ++) {
10
- if (nums [i ] != nums [j ]) {
11
- i ++;
12
- nums [i ] = nums [j ];
7
+ public static class Solution1 {
8
+ /**
9
+ * Key: It doesn't matter what you leave beyond the returned length.
10
+ */
11
+ public int removeDuplicates (int [] nums ) {
12
+ int i = 0 ;
13
+ for (int j = 1 ; j < nums .length ; j ++) {
14
+ if (nums [i ] != nums [j ]) {
15
+ i ++;
16
+ nums [i ] = nums [j ];
17
+ }
18
+ }
19
+ return i + 1 ;
13
20
}
14
- }
15
- return i + 1 ;
16
21
}
17
- }
22
+
23
+ public static class Solution2 {
24
+ /**
25
+ * My completely original solution on 2/2/2022.
26
+ */
27
+ public int removeDuplicates (int [] nums ) {
28
+ int left = 0 ;
29
+ int right = 1 ;
30
+ for (; right < nums .length ; right ++) {
31
+ while (right < nums .length && nums [right ] == nums [right - 1 ]) {
32
+ right ++;
33
+ }
34
+ if (right < nums .length ) {
35
+ nums [++left ] = nums [right ];
36
+ }
37
+ }
38
+ CommonUtils .printArray (nums );
39
+ return left + 1 ;
40
+ }
41
+ }
42
+
18
43
}
Original file line number Diff line number Diff line change 8
8
9
9
public class _26Test {
10
10
private static _26 .Solution1 solution1 ;
11
+ private static _26 .Solution2 solution2 ;
11
12
private static int [] nums ;
12
13
13
14
@ BeforeClass
14
15
public static void setup () {
15
16
solution1 = new _26 .Solution1 ();
17
+ solution2 = new _26 .Solution2 ();
16
18
}
17
19
18
20
@ Test
19
21
public void test1 () {
20
22
nums = new int []{1 , 1 , 2 };
21
23
assertEquals (2 , solution1 .removeDuplicates (nums ));
24
+ assertEquals (2 , solution2 .removeDuplicates (nums ));
22
25
}
23
26
24
27
@ Test
You can’t perform that action at this time.
0 commit comments