algorithm-essentials icon indicating copy to clipboard operation
algorithm-essentials copied to clipboard

算法精粹 - Remove Duplicates from Sorted Array II 示例代码似乎有误

Open eric-yt opened this issue 9 years ago • 1 comments

首先,谢谢提供如此好的一个教程帮助初学者或像我这种忘记的差不多的人重新学习。 示例代码似乎有误,我在leecode中尝试了,不能通过,因为nums中的元素被替换,所以好比[1,1,1,2,2,3]这样的list会出错。 我找到一个稍微简单的办法处理这种问题,直接删除list中重复的元素,示例代码如下(n=2,使用其他n>2的情况): class Solution(object): def removeDuplicates(self, nums): """ :type nums: List[int] :rtype: int """ if len(nums) <= 2: return len(nums) idx = 2 while idx < len(nums): if nums[idx-2]== nums[idx]: del nums[idx] else: idx+=1 return len(nums)

eric-yt avatar Jan 09 '17 21:01 eric-yt

cpp version根本没有occur这个变量

eggsalot avatar Jul 17 '17 01:07 eggsalot