Find All Numbers Disappeared in an Array

Posted 唐僧洗发爱飘柔

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Find All Numbers Disappeared in an Array相关的知识,希望对你有一定的参考价值。

    这道题为简单题

  题目:

    

 

  思路:

    这道题其实很简单,但是我之前做出来总是超时,于是就优化了一下代码,创建一个列表a,长度为len(nums)+1初始化为0,遍历列表nums,把对应列表a的索引变为1,最后用列表生成式返回a[i]为0的索引

  代码:

class Solution(object):
    def findDisappearedNumbers(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        a = [0] * (len(nums)+1)
        for i in nums:
            a[i] = 1
        return [i for i in range(1,len(nums)+1) if a[i] == 0]

 

以上是关于Find All Numbers Disappeared in an Array的主要内容,如果未能解决你的问题,请参考以下文章

Find All Numbers Disappeared in an Array

Find All Numbers Disappeared in an Array

Find All Numbers Disappeared in an Array

Find All Numbers Disappeared in an Array

448. Find All Numbers Disappeared in an Array

448. Find All Numbers Disappeared in an Array