美文网首页
【Leetcode】【Python】448. Find All

【Leetcode】【Python】448. Find All

作者: 小歪与大白兔 | 来源:发表于2017-10-25 22:44 被阅读0次

    问题描述:

    image.png

    输入:[4,3,2,7,8,2,3,1]
    输出:[5,6]

    代码示例一:

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

    代码示例二:不占用额外内存

    class Solution(object):
        def findDisappearedNumbers(self, nums):
            """
            :type nums: List[int]
            :rtype: List[int]
            """
            result = []
            for i in range(len(nums)):
    
                count = abs(nums[i])
                nums[count - 1] = - abs(nums[count - 1])
    
            for i in range(len(nums)):
                if nums[i] > 0:
                    result.append(i+1)
            return result
    

    相关文章

      网友评论

          本文标题:【Leetcode】【Python】448. Find All

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