26. Remove Duplicates from Sorted Array
程序员文章站
2024-02-17 12:51:58
...
- 时间复杂度O(n),空间复杂度O(1):
class Solution(object):
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
i = 0
if len(nums)<1:
return 0
else:
while i< len(nums)-1:
if nums[i] == nums[i+1]:
nums.remove(nums[i])
else:
i += 1
return len(nums)
- 时间复杂度O(1),空间复杂度O(1): python自带的set函数,转化成集合类型
class Solution(object):
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
res = list(set(nums))
nums[:] = sorted(res)
return len(nums)
推荐阅读
-
26. Remove Duplicates from Sorted Array
-
26. Remove Duplicates from Sorted Array
-
26. Remove Duplicates from Sorted Array
-
26. Remove Duplicates from Sorted Array
-
26. Remove Duplicates from Sorted Array
-
#26. Remove Duplicates from Sorted Array
-
26. Remove Duplicates from Sorted Array
-
26. Remove Duplicates from Sorted Array
-
leetcode82: Remove Duplicates from Sorted ListII
-
【一天一道LeetCode】#26. Remove Duplicates from Sorted Array