remove_array_duplicates.py 618 B

1234567891011121314151617181920212223242526272829303132
  1. from typing import List
  2. def removeDuplicates(nums: List[int]):
  3. # 暴力解法
  4. lens = len(nums)
  5. # i = 0
  6. # while i < lens-1:
  7. # if nums[i] == nums[i + 1]:
  8. # nums.remove(nums[i])
  9. # lens -= 1
  10. # while i != 0:
  11. # i -= 1
  12. # else:
  13. # i += 1
  14. # return len(nums)
  15. # 快慢指针法
  16. if not nums:
  17. return 0
  18. slow = 1
  19. for fast in range(1,lens):
  20. if nums[fast] != nums[fast - 1]:
  21. nums[slow] = nums[fast]
  22. slow += 1
  23. return slow
  24. print(removeDuplicates([0, 0, 1, 1, 2, 3, 3]))