- Find All Numbers Disappeared in an Array
思路:Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space. 不占用额外的空间并且时间复杂度在O(n)。把每个数减一,将其作为所谓的数组下标,出现的就取负,不出现的依旧是正数,如此找到缺失的整数。
class Solution(object):
def findDisappearedNumbers(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
for i in range(len(nums)):
index = abs(nums[i])-1
nums[index] = - abs(nums[index])
return [i +1 for i in range(len(nums)) if nums[i]>0]
好枯燥,得换种学习方式了,半天也没啥长进。