Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Published

...

https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/description/

Status
colourBlue
title待复习

Code Block
languagego
func removeDuplicates(nums []int) int {
    slow, fast := 0, 0
    for fast < len(nums) {
        // fmt.Printf("slow = %d, fast = %d | %v | %v\n",
        //     slow, fast, nums[:slow], nums,
        // )
        if  slow-2 < 0 || nums[fast] != nums[slow-1] || nums[fast] != nums[slow-2] {
            // new element
            nums[slow] = nums[fast]
            slow++
        } 
        
        fast++
    }

    return slow
}