1.我疏忽的地方:Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory。所以我最初重新开辟空间出错了。正确做法:
class Solution(object):
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
while(val in set(nums)): #这里的set()用得特别好,能大大减小循环;而且我试了不加set()的话运行特别特别的慢。
nums.remove(val)
return len(nums)
2.另外,在debug的过程中发现,while特别容易出现死循环,只要不小心写错了啥,就等着跳不出来吧。
2019/4/10 16:00