https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/

给出一列数,1 ≤ a[i] ≤ n,n是数组大小,有些数出现两次,有些数出现一次,找出在[1,n]中但是不在数列中的数。

不用额外的空间,时间复杂度O(n)

Example:

Input:
[4,3,2,7,8,2,3,1] Output:
[5,6] 解题思路:
一开始想的很简单
1、把原数组去重
2、构造一个[1,n]的数组,然后求个差集就行了 既然都是用python,set这个数据结构简直就是去重神器,直接set(nums)就去重
求差积的话list比较麻烦,set相减可以直接求差积
最后return要求是list数据结构,转回来就行
class Solution(object):
def findDisappearedNumbers(self, nums):
return list(set(range(1, len(nums) + 1)) - set(nums))
ps1.不要做一边append/remove这种操作一边遍历数组,常常会越界,宁愿复制出来一个再操作
ps2.在py2.7里面range()返回一整個list,xrange()返回一个生成器,后者在空间效率上高很多,大多数情况下无脑用xrange()就可以了。
py3就没这个问题,因为机智的让range()就是老xrange(),然后干掉了老range().

448. Find All Numbers Disappeared in an Array的更多相关文章

  1. 448. Find All Numbers Disappeared in an Array&&645. Set Mismatch

    题目: 448. Find All Numbers Disappeared in an Array Given an array of integers where 1 ≤ a[i] ≤ n (n = ...

  2. 【leetcode】448. Find All Numbers Disappeared in an Array

    problem 448. Find All Numbers Disappeared in an Array solution: class Solution { public: vector<i ...

  3. leetcode 217. Contains Duplicate 287. Find the Duplicate Number 442. Find All Duplicates in an Array 448. Find All Numbers Disappeared in an Array

    后面3个题都是限制在1-n的,所有可以不先排序,可以利用巧方法做.最后两个题几乎一模一样. 217. Contains Duplicate class Solution { public: bool ...

  4. 448. Find All Numbers Disappeared in an Array【easy】

    448. Find All Numbers Disappeared in an Array[easy] Given an array of integers where 1 ≤ a[i] ≤ n (n ...

  5. 448. Find All Numbers Disappeared in an Array@python

    Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ot ...

  6. 5. 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 ot ...

  7. 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 ot ...

  8. leetcode 448. Find All Numbers Disappeared in an Array -easy (重要)

    题目链接: https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/ 题目描述: Give ...

  9. 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 a ...

  10. [LeetCode&Python] Problem 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 ot ...

随机推荐

  1. 关于Django 错误 doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS

    记录一下 报错 doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS\ 这个问题出现没 ...

  2. 3sum问题的解决

    其实一开始想错了,把这个问题想难了,导致没有思路,现在好了很多. 题目: Given an array S of n integers, are there elements a, b, c in S ...

  3. 重复加载同一个jqgrid

    重复加载同一个jqgrid时需要先清除原先的数据,再进行加载新的数据: 清除时使用方法:jQuery.jgrid.gridUnload('jqGridId'); 同时还有一个GridDestroy的方 ...

  4. BZOJ 2039: [2009国家集训队]employ人员雇佣

    2039: [2009国家集训队]employ人员雇佣 Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 1369  Solved: 667[Submit ...

  5. NOIP2016题解

    D1T1:把方向和朝向异或一下,在mod n意义下+1s或-1s. #include<cstdio> const int N=1e5+5; int n,m,j,k,v,s[N]; char ...

  6. SNMP Tutorial

    Applications: Internet Management (SNMP) 30.1 Introduction 30.2 The Level Of Management Protocols 30 ...

  7. Spring MVC学习笔记——引入静态文件

    1.在user-servlet.xml中加入以下代码,才能使得对静态文件的请求不被Controller捕获,而映射到一个固定的地址 <!-- 将静态文件指定到某个特殊的文件夹中统一处理 --&g ...

  8. 编译安装 Centos 7 x64 + tengine.2.0.3 (实测+笔记)

    系统硬件:vmware vsphere (CPU:2*4核,内存2G) 系统版本:CentOS Linux release 7.0.1406 安装步骤: 1.系统环境 1.1 更新系统 [root@c ...

  9. Java集合之ArrayList

    ArrayList ArrayList是最常见以及每个Java开发者最熟悉的集合类了,顾名思义,ArrayList就是一个以数组形式实现的集合,以一张表格来看一下ArrayList里面有哪些基本的元素 ...

  10. web中c#纯网站中引用log4net模块,不记录日志

    如题,解决如下: 1.log4net.config配置如下: <?xml version="1.0" encoding="utf-8" ?> < ...