Leetcode 还未解决的bug

Posted 切力

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 还未解决的bug相关的知识,希望对你有一定的参考价值。

27. Remove Element

val = 1
nums = [1,1,2,3]
for i in nums:
    if i == val:
        nums.remove(i)

a = nums

result :
val = 1
nums = [1,1,2,3]
for i in nums[:]:
    if i == val:
        nums.remove(i)

a = nums

result:
不用remove的解法:

class
Solution(object): def removeElement(self, nums, val): """ :type nums: List[int] :rtype: int """ if not nums: return 0 tail = 0 for i in range(len(nums)): if nums[i]!=val: nums[tail]=nums[i] tail+=1 return tail

 当 return错误数 的时候,有可能会产生index out of range

 

以上是关于Leetcode 还未解决的bug的主要内容,如果未能解决你的问题,请参考以下文章