-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathRemoveDuplicatesFromSortedArrayII.java
More file actions
38 lines (34 loc) · 1.02 KB
/
Copy pathRemoveDuplicatesFromSortedArrayII.java
File metadata and controls
38 lines (34 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* Follow up for {@link RemoveDuplicatesFromSortedArray}:
* What if duplicates are allowed at most twice?
* <p>
* For example,
* Given sorted array nums = [1,1,1,2,2,3],
* <p>
* Your function should return length = 5, 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.
* <p>
* Accepted.
*/
public class RemoveDuplicatesFromSortedArrayII {
public int removeDuplicates(int[] nums) {
if (nums == null) {
return 0;
}
if (nums.length <= 2) {
return nums.length;
}
int count = 0, i = 0;
while (i + 2 < nums.length - count) {
if (nums[i + 1] == nums[i] && nums[i + 2] == nums[i]) {
count++;
if (i + 3 <= nums.length - count) {
System.arraycopy(nums, i + 3, nums, i + 2, nums.length - count - (i + 2));
}
} else {
i++;
}
}
return nums.length - count;
}
}