Given an array nums of n integers where nums[i] is in the range [1, n], return an array of all the integers in the range [1, n] that do not appear in nums.
Example 1: Input: nums = [4,3,2,7,8,2,3,1] Output: [5,6]
- code
class Solution:
def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
return set([i for i in range(1, len(nums) + 1)]) - set(nums)
- code
class Solution:
def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
for n in nums:
a = abs(n) - 1
if nums[a] > 0: nums[a] *= -1
return [i+1 for i in range(len(nums)) if nums[i] > 0]
- code
class Solution:
def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
allnums = list(range(1, len(nums)+1))
for v in set(nums):
allnums[v-1] = 0
return [num for num in allnums if num != 0]
- code
class Solution:
def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
seen = set(nums)
output = []
for num in range(1,len(nums)+1):
if num not in seen:
output.append(num)
return output