解题思路
这题很简单
ahead指向第一个未处理单词
curr指向最后一个已经处理的保留单词
clear表示是不是已经重复过一次了
然后一遍扫描
80. 删除有序数组中的重复项 II
代码
class Solution(object):
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
curr, ahead, clear = 0, 1, False
while ahead < len(nums):
if nums[ahead] != nums[curr]:
curr += 1
nums[curr] = nums[ahead]
ahead += 1
clear = False
elif not clear: # repeat once
curr += 1
nums[curr] = nums[ahead]
ahead += 1
clear = True
else: # repeat more than once
ahead += 1
return curr + 1
效果图
网友评论