Two Pointers
Better Solution
2.05 %
class Solution {
public int removeDuplicates(int[] nums) {
int p = 0;
int count = 0;
for (int i = 0; i < nums.length; i++) {
if (i == 0 || nums[i] != nums[i - 1]) {
count = 0;
} else {
count++;
}
if (count < 2) {
nums[p] = nums[i];
p++;
}
}
return p;
}
}
2.05 %
class Solution {
public int removeDuplicates(int[] nums) {
int p = 0;
int i = 0;
int start = 0;
while (i < nums.length) {
start = i;
while (i < nums.length && nums[i] == nums[start]) {
if (i - start < 2) {
nums[p] = nums[i];
p++;
}
i++;
}
}
return p;
}
}
No comments:
Post a Comment