LeetCode 448 Find All Numbers Disappeared in an Array 解题报告
Posted yao1996
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 解题报告的主要内容,如果未能解决你的问题,请参考以下文章
#Leetcode# 448. Find All Numbers Disappeared in an Array
LeetCode 448. Find All Numbers Disappeared in an Array
LeetCode 448. Find All Numbers Disappeared in an Array
LeetCode - 448. Find All Numbers Disappeared in an Array