LeetCode Find All Numbers Disappeared in an Array

来源:互联网 发布:svn服务器搭建ubuntu 编辑:程序博客网 时间:2024/05/29 07:39

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-8class 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
0 0
原创粉丝点击