leetcode: 删除排序数组中的重复项

给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。

不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。

这个的思路很清楚,就简单的遍历一遍,把前几个数字的内容替换就好

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution(object):
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums) == 0:
return 0
count = 0
for i in nums:
if nums[count] != i:
count += 1
nums[count] = i

return count+1

这题比较简单,就到这里吧。

唉,我是真的懒。。。明天继续