harrytothemoon / leetcodeAplus

Leetcode meeting note
2 stars 0 forks source link

[80] Remove Duplicates from Sorted Array II #65

Open tsungtingdu opened 3 years ago

tsungtingdu commented 3 years ago
var removeDuplicates = function(nums) {
    let index = 1
    let current = nums[0]
    let count = 1

    for (let i = 1; i < nums.length; i++) {
      if (nums[i] === current && count < 2) {
        nums[index] = current
        index++
        count++
      } else if (nums[i] === current && count === 2) {
        continue
      } else {
        count = 1
        current = nums[i]
        nums[index] = current
        index++
      }
    }
    return index
};
harrytothemoon commented 3 years ago
var removeDuplicates = function(nums) {
    let pointer1 = 2
    for (let pointer2 = 2; pointer2 < nums.length; pointer2++) {
        if (nums[pointer1 - 2] !== nums[pointer2]) {
            nums[pointer1] = nums[pointer2]
            pointer1++
        }
    }
    return pointer1
};