80 Remove Duplicates from Sorted Array II
1. Question
2. Implementation
class Solution {
public int removeDuplicates(int[] nums) {
if (nums.length <= 2) {
return nums.length;
}
int count = 1, index = 1;
for (int i = 1; i < nums.length; i++) {
if (nums[i - 1] != nums[i]) {
count = 1;
nums[index++] = nums[i];
}
else if (count < 2) {
nums[index++] = nums[i];
++count;
}
}
return index;
}
}3. Time & Space Complexity
Last updated