Table of Content
这道题比较简单,值得注意的是空间复杂度的要求。这里提供两种解法,第一种解法效率比较差,但没有用另外的list。第二章解法利用set进行去重,效率比较高。
参考代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
class Solution: def removeDuplicates(self, nums): """ :type nums: List[int] :rtype: int """ if len(nums) == 0 or len(nums) == 1: return len(nums) i = 1 while i < len(nums): if nums[i] == nums[i-1]: nums.remove(nums[i]) continue i = i + 1 return len(nums) class Solution2: def removeDuplicates(self, nums): """ :type nums: List[int] :rtype: int """ l = sorted(list(set(nums))) if len(l): for i in range(0, len(l)): nums[i] = l[i] return len(l) |
如果您有好的建议,欢迎来信与我交流

也欢迎关注微信公众号“苔原带”

