ほぼ日刊競プロ leetcode 448. Find All Numbers Disappeared in an Array
448. Find All Numbers Disappeared in an Array
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.
考えたこと
オリジナルのnumsの数の欠損していたり重複していない理想的なtempを作る.あとはsetして引き算または対称差集合(集合同士の積)をとれば良いと考えた.
class Solution:
def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
nums = sorted(nums)
temp = [i for i in range(1,len(nums)+1)]
ans = set(nums)^set(temp)
#ans = set(temp)-set(nums)
return list(ans)