File tree Expand file tree Collapse file tree 1 file changed +31
-27
lines changed
src/main/java/com/fishercoder/solutions Expand file tree Collapse file tree 1 file changed +31
-27
lines changed Original file line number Diff line number Diff line change 1
1
package com .fishercoder .solutions ;
2
2
3
3
import java .util .ArrayList ;
4
+ import java .util .List ;
4
5
5
6
/**
7
+ * 80. Remove Duplicates from Sorted Array II
8
+ *
6
9
* Follow up for "Remove Duplicates":
7
10
What if duplicates are allowed at most twice?
8
11
14
17
*/
15
18
public class _80 {
16
19
20
+ public static class Solution1 {
17
21
public int removeDuplicates (int [] nums ) {
18
- int counter = 0 ;
19
- int len = nums .length ;
20
- if (len == 0 ) {
21
- return 0 ;
22
- }
23
- if (len == 1 ) {
24
- return 1 ;
25
- }
26
- if (len == 2 ) {
27
- return 2 ;
28
- }
29
-
30
- ArrayList <Integer > a = new ArrayList ();
31
- a .add (nums [0 ]);
32
- a .add (nums [1 ]);
33
- for (int i = 2 ; i < len ; i ++) {
34
- if (nums [i ] != nums [i - 1 ]) {
35
- a .add (nums [i ]);
36
- } else if (nums [i ] != nums [i - 2 ]) {
37
- a .add (nums [i ]);
38
- }
39
- }
40
-
41
- counter = a .size ();
42
- for (int i = 0 ; i < counter ; i ++) {
43
- nums [i ] = a .get (i );
22
+ int counter = 0 ;
23
+ int len = nums .length ;
24
+ if (len == 0 ) {
25
+ return 0 ;
26
+ }
27
+ if (len == 1 ) {
28
+ return 1 ;
29
+ }
30
+ if (len == 2 ) {
31
+ return 2 ;
32
+ }
33
+
34
+ List <Integer > a = new ArrayList ();
35
+ a .add (nums [0 ]);
36
+ a .add (nums [1 ]);
37
+ for (int i = 2 ; i < len ; i ++) {
38
+ if (nums [i ] != nums [i - 1 ]) {
39
+ a .add (nums [i ]);
40
+ } else if (nums [i ] != nums [i - 2 ]) {
41
+ a .add (nums [i ]);
44
42
}
43
+ }
45
44
46
- return counter ;
45
+ counter = a .size ();
46
+ for (int i = 0 ; i < counter ; i ++) {
47
+ nums [i ] = a .get (i );
48
+ }
49
+ return counter ;
47
50
}
51
+ }
48
52
49
53
}
You can’t perform that action at this time.
0 commit comments