使用del删除list里的元素
class Solution: # @param {integer[]} nums # @return {integer} def removeDuplicates(self, nums): i, j = 0, 1 if len(nums)<2: return len(nums) while(i < len(nums)-1): j = i+1 if nums[i] == nums[j]: del nums[j] else: i += 1 return len(nums)
不改变list
class Solution: # @param {integer[]} nums # @return {integer} def removeDuplicates(self, nums): l, i = 1, 1 if len(nums)<2: return len(nums) for i in range(1,len(nums)): if nums[i]!=nums[i-1]: nums[l] = nums[i] l += 1 return l