Skip to content

Commit ee43c94

Browse files
refactor 80
1 parent bb4312c commit ee43c94

File tree

1 file changed

+30
-42
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+30
-42
lines changed

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

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,39 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6-
/**
7-
* 80. Remove Duplicates from Sorted Array II
8-
*
9-
* Follow up for "Remove Duplicates":
10-
What if duplicates are allowed at most twice?
11-
12-
For example,
13-
Given sorted array nums = [1,1,1,2,2,3],
14-
15-
Your function should return length = 5,
16-
with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn't matter what you leave beyond the new length.
17-
*/
186
public class _80 {
197

20-
public static class Solution1 {
21-
public int removeDuplicates(int[] nums) {
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]);
8+
public static class Solution1 {
9+
public int removeDuplicates(int[] nums) {
10+
int counter = 0;
11+
int len = nums.length;
12+
if (len == 0) {
13+
return 0;
14+
}
15+
if (len == 1) {
16+
return 1;
17+
}
18+
if (len == 2) {
19+
return 2;
20+
}
21+
22+
List<Integer> a = new ArrayList();
23+
a.add(nums[0]);
24+
a.add(nums[1]);
25+
for (int i = 2; i < len; i++) {
26+
if (nums[i] != nums[i - 1]) {
27+
a.add(nums[i]);
28+
} else if (nums[i] != nums[i - 2]) {
29+
a.add(nums[i]);
30+
}
31+
}
32+
33+
counter = a.size();
34+
for (int i = 0; i < counter; i++) {
35+
nums[i] = a.get(i);
36+
}
37+
return counter;
4238
}
43-
}
44-
45-
counter = a.size();
46-
for (int i = 0; i < counter; i++) {
47-
nums[i] = a.get(i);
48-
}
49-
return counter;
5039
}
51-
}
5240

5341
}

0 commit comments

Comments
 (0)