Skip to content

Latest commit

 

History

History
25 lines (17 loc) · 504 Bytes

80. 删除有序数组中的重复项 II.md

File metadata and controls

25 lines (17 loc) · 504 Bytes
  • 双指针
function removeDuplicates(nums: number[]): number {

  const { length } = nums;

  let fast = 2, slow = 2;

  while (fast < length) {
    // 只要不满足重复的条件,就可以往前排
    if (nums[fast] !== nums[slow - 1] || nums[fast] !== nums[slow - 2]) {
      nums[slow] = nums[fast];
      slow++;
    }
    ++fast;
  }

  return slow;

};