之后把程序中
while fast <= len(nums):
改为
while fast < len(nums):
就又不报错了
class Solution(object):
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums:
return 0
slow = 0
fast = 0
while fast <= len(nums):
if nums[fast] != nums[slow]:
slow += 1
nums[slow] = nums[fast]
fast += 1
else:
fast += 1
return slow+1
fast当然不能等于数组长度。因为数组下标范围是从0到len(nums)-1。你等于len(nums-1)就下标越界了
楼主是初学者吧