首页 > 其他 > 详细

Leetcode 还未解决的bug

时间:2017-11-12 22:53:43      阅读:267      评论:0      收藏:0      [点我收藏+]

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

原文:http://www.cnblogs.com/developerchen/p/7823263.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!