Skip to content

Commit 3f44e8c

Browse files
refactor 80
1 parent 5eee670 commit 3f44e8c

File tree

1 file changed

+31
-27
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+31
-27
lines changed
Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.fishercoder.solutions;
22

33
import java.util.ArrayList;
4+
import java.util.List;
45

56
/**
7+
* 80. Remove Duplicates from Sorted Array II
8+
*
69
* Follow up for "Remove Duplicates":
710
What if duplicates are allowed at most twice?
811
@@ -14,36 +17,37 @@
1417
*/
1518
public class _80 {
1619

20+
public static class Solution1 {
1721
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]);
4442
}
43+
}
4544

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;
4750
}
51+
}
4852

4953
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy