80. Remove Duplicates from Sorted Array II
Approach
two pointer solution
Complexity
Time complexity: O(n)
Space complexity: o(1)
Code
class Solution {
public int removeDuplicates(int[] nums) {
if(nums.length <= 2){
return nums.length;
}
int j = 2;
for(int i = 2; i < nums.length; i++){
if(nums[i] != nums[j-2]){
nums[j] = nums[i];
j++;
}
}
return j;
}
}
Comments
Post a Comment