Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
eric-yt opened this issue Jan 9, 2017 · 1 comment

Comments

@eric-yt
Copy link

eric-yt commented Jan 9, 2017

首先,谢谢提供如此好的一个教程帮助初学者或像我这种忘记的差不多的人重新学习。
示例代码似乎有误,我在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)

@eggsalot
Copy link

cpp version根本没有occur这个变量

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants