Day36

作者: wendy_要努力努力再努力 | 来源:发表于2017-12-26 11:12 被阅读0次
  1. 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]

好枯燥,得换种学习方式了,半天也没啥长进。

相关文章

网友评论

      本文标题:Day36

      本文链接:https://www.haomeiwen.com/subject/yvedgxtx.html