class Solution: # 1 2 2 3 4 def removeDuplicates(self, nums): nums_len = len(nums) # 5 if nums_len == 0: return nums_len if nums_len == 1: return nums_len flag=True i=0 while flag: nums_len=len(nums) if nums_len==1: return nums_len if nums[i] == nums[i + 1]:#nums = [1,1] del nums[i + 1] i=0 continue i+=1 nums_len=len(nums) if i==nums_len-1: flag=False return nums_len
上面是手打
下面是GPT改进后的
class Solution: # 1 2 2 3 4 def removeDuplicates(self, nums): nums_len = len(nums) #3 if nums_len <= 1: return nums_len i = 0 while i < nums_len - 1:#1 2 2 if nums[i] == nums[i + 1]: del nums[i + 1] nums_len -= 1 else: i += 1 return nums_len
标签:return,删除,nums,leedcode,len,flag,数组,removeDuplicates From: https://www.cnblogs.com/yyyjw/p/17976540