[LeetCode]#448. Find All Numbers Disappeared in an Array

Fatboy Slim
1 min readApr 2, 2020

--

Environment: Python 3.7

Key technique: set

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]

Analysis:

Use “set” function to search current numbers and use this list to find the disappeared number.

Solution:

class Solution:
def findDisappearedNumbers(self, nums):
find=set(nums)
l=len(nums)
ans=[]
for i in range(1,l+1):
if i not in find:
ans.append(i)
return ans

Submitted result:

Reference:

https://blog.csdn.net/fuxuemingzhu/article/details/53981307

--

--

Fatboy Slim
Fatboy Slim

Written by Fatboy Slim

Interesting in any computer science.

No responses yet