LeetCode 448 Find All Numbers Disappeared in an Array 解题报告

题目要求

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.

题目分析及思路

给定一个整数数组,数组元素取值在1-n之间(n为数组长度)。有些元素出现了两次,有些只出现了一次。要求得到在范围1-n之间没有出现在数组中的值。可以使用集合来做,将1-n这个范围和给定数组转成集合,使用集合的差得到最后的结果。

python代码

class Solution:

    def findDisappearedNumbers(self, nums: List[int]) -> List[int]:

        return list(set(range(1,len(nums)+1))-set(nums))

        

 

LeetCode 448 Find All Numbers Disappeared in an Array 解题报告

上一篇:pandas apply 草稿,等待合集


下一篇:error C1189: #error : Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version. Please #define _AFXDLL or do not use /MD[d]