Gary Lai
  • Home
  • About
Sign in Subscribe

26. Remove Duplicates from Sorted Array

Last updated on  Jul 21, 2024

26. Remove Duplicates from Sorted Array

題目的標題寫的並不是很清楚,這個題目其實是要把

雙指針問題

class Solution:
    def removeDuplicates(self, nums: List[int]) -> int:
        slow = 0
        fast = 0

        while fast < len(nums):
            if nums[fast] != nums[slow]:
                slow += 1
                nums[slow] = nums[fast]
            fast += 1

        return slow + 1
Previous 424. Longest Repeating Character Replacement
Next 209. Minimum Size Subarray Sum
Gary Lai © 2025
  • Sign up
Powered by Ghost