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))

原文地址:https://www.cnblogs.com/yao1996/p/10646599.html

时间: 2024-10-11 19:48:26

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 Add to List Description Submission Solutions Total Accepted: 31266 Total Submissions: 58997 Difficulty: Easy Contributors: yuhaowang001 Given an array of integers where 1 ≤ a[i] ≤ n (n = size of

Leetcode 448. Find All Numbers Disappeared in an Array JAVA语言151. Reverse Words in a String

Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". Update (2015-02-12): For C programmers: Try to solve it in-place in O(1) space. 题意:反转字符串中的单词,注意空格的处理!!! public 

leetcode之Find All Numbers Disappeared in an Array

问题来源:Find All Numbers Disappeared in an Array 很久没有刷题了,感觉大脑开始迟钝,所以决定重拾刷题的乐趣.一开始不要太难,选一些通过率高的题目做,然后就看到了这个题目.我有些吃惊,这个题我虽然知道两种解法,但本身还是有难度的,居然通过率这么高.然后就搜索相关网页,看到一个和它很接近的题目<Find All Duplicates in an Array>,然后就释然了.这两个题目有相同的题干,只是问题略微不同,解法有相似之处.估计是因为题号太接近了,会

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) runti

448. Find All Numbers Disappeared in an Array Add to List

题目描述 题目分析 有个[1,n]的条件要充分利用起来. 题目代码 public class Solution { public List<Integer> findDisappearedNumbers(int[] nums) { List<Integer> list=new ArrayList<Integer>(); if(nums==null || nums.length==0) return list; if(nums.length == 1 ){ list.ad

[Array]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?

LeetCode: Search in Rotated Sorted Array 解题报告

Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise retu

【LeetCode】Remove Duplicates from Sorted Array 解题报告

[LeetCode]Remove Duplicates from Sorted Array 解题报告 标签(空格分隔): LeetCode [LeetCode] https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Total Accepted: 129010 Total Submissions: 384622 Difficulty: Easy Question Given a sorted array, remov

【LeetCode】103. Binary Tree Zigzag Level Order Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51524241 Subject 出处:https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ri