1234567891011121314151617181920212223242526272829303132 |
- from typing import List
- def removeDuplicates(nums: List[int]):
- # 暴力解法
- lens = len(nums)
- # i = 0
- # while i < lens-1:
- # if nums[i] == nums[i + 1]:
- # nums.remove(nums[i])
- # lens -= 1
- # while i != 0:
- # i -= 1
- # else:
- # i += 1
- # return len(nums)
- # 快慢指针法
- if not nums:
- return 0
- slow = 1
- for fast in range(1,lens):
- if nums[fast] != nums[fast - 1]:
- nums[slow] = nums[fast]
- slow += 1
- return slow
- print(removeDuplicates([0, 0, 1, 1, 2, 3, 3]))
|