LeetCode Find All Numbers Disappeared in an Array

Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.

Find all the elements of [1, n] inclusive that do not appear in this 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.

Example:

Input:
[4,3,2,7,8,2,3,1]

Output:
[5,6]

#coding=utf-8
class Solution(object):
    def findDisappearedNumbers(self, nums):
        """
        :type nums: list[int]
        :rtype: list[int]
        """
        n = len(nums)
        s = set(nums) #已出現的數字
        res = [i for i in xrange(1,n+1)]
        res = set(res)
        for i in s:
            res.remove(i)
        return list(res) # beats 92.6% 268ms
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章