Skip to content

Commit 2bc2fd9

Browse files
authored
Merge pull request #3038 from benmak11/remove-dups
Create 0080-remove-duplicates-from-sorted-array-ii.java
2 parents 317d899 + b1b81b2 commit 2bc2fd9

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Diff for: java/0080-remove-duplicates-from-sorted-array-ii.java

+22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
class Solution {
2+
public int removeDuplicates(int[] nums) {
3+
int l = 0, r = 0, n = nums.length;
4+
5+
while (r < n) {
6+
int count = 1;
7+
while (r + 1 < n && nums[r] == nums[r + 1]) {
8+
r++;
9+
count++;
10+
}
11+
12+
for (int i = 0; i < Math.min(2, count); i++) {
13+
nums[l] = nums[r];
14+
l++;
15+
}
16+
r++;
17+
}
18+
return l;
19+
}
20+
}
21+
122
/*
223
* Time Complexity: O(n);
324
* Space Complexity: O(1);
@@ -36,3 +57,4 @@ public int removeDuplicates(int[] nums) {
3657
}
3758
}
3859

60+

0 commit comments

Comments
 (0)