-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path080_RemoveDuplicatesFromSortedArrayII80.java
50 lines (46 loc) · 1.33 KB
/
080_RemoveDuplicatesFromSortedArrayII80.java
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
39
40
41
42
43
44
45
46
47
48
49
50
/**
* Follow up for "Remove Duplicates":
* What if duplicates are allowed at most twice?
*
* For example,
* Given sorted array nums = [1,1,1,2,2,3],
*
* 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.
*
*/
public class RemoveDuplicatesFromSortedArrayII80 {
public int removeDuplicates(int[] nums) {
int count = 1;
int i = 0;
for (int j=1; j<nums.length; j++) {
if (nums[i] != nums[j]) {
i++;
nums[i] = nums[j];
count = 1;
} else if (count < 2) {
count++;
i++;
nums[i] = nums[j];
}
}
return i+1;
}
/**
* https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27987/Short-and-Simple-Java-solution-(easy-to-understand)
*/
public int removeDuplicates2(int[] nums) {
int i = 0;
for (int n : nums)
if (i < 2 || n > nums[i - 2])
nums[i++] = n;
return i;
}
// private int removeDuplicates(int[] nums, int k) {
// int i = 0;
// for (int n : nums)
// if (i < k || n > nums[i-k])
// nums[i++] = n;
// return i;
// }
}