Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.php
Find all the elements of [1, n] inclusive that do not appear in this array.ios
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:app
Input:
[4,3,2,7,8,2,3,1]
Output:
[5,6]
复制代码
就是有一个包含 1 到 n 的数字列表中,缺乏哪些数字,直接用 set 计算差集便可,时间复杂度 O(N),空间复杂度 O(N)。less
class Solution(object):
def findDisappearedNumbers(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
new = range(1,len(nums)+1)
return list(set(new)-set(nums))
复制代码
Runtime: 320 ms, faster than 89.85% of Python online submissions for Find All Numbers Disappeared in an Array.
Memory Usage: 20.6 MB, less than 30.78% of Python online submissions for Find All Numbers Disappeared in an Array.
复制代码
每日格言:过去属于死神,将来属于你本身。yii