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. day7_subprocess模块和面向对象,反射

    常用subprocess方法示例 #执行命令,返回命令执行状态 , 0 or 非0>>> retcode = subprocess.call(["ls", &qu ...

  2. CSS选 择器 三种样式

    一.CSS三种样式 代码 宽度 高度 实线 颜色  A内联样式是优先级最高的方式 px必须写 A:内联式  弊端:代码多很乱 <body> <div class="divc ...

  3. 红米3 SM71.1(android-7.1.1_r6)更新发布20161229年末增强版

    一.写在前面 我只是个人爱好,本ROM未集成任何第三方推广软件,我只是喜欢把好的资源分享出来,若可以,我们一起学习,一起进步. 请不要问我怎么刷机! 请不要问我玩游戏卡不卡(有钱你就换好点的手机)! ...

  4. android获取位置location为null的问题

      12:38:542016-12-23 很多人经常遇到这种问题,主要是获取到位置的信息为null,第一个主要要有权限 <uses-permission android:name="a ...

  5. IIS配置MP3/MP4/OGG/flv等资源文件访问

    配置过程参考:http://www.cnblogs.com/EasonJim/p/4752399.html 以下包含了mp4的mime类型: 323 text/h323 acx application ...

  6. 企业SOA架构设计理论

    SOA简介 SOA(Service-Oriented Architecture,面向服务架构)是一种将信息系统模块化为服务的架构风格.拥有了服务之后,我们就可以迅速地将这些服务按不同方式重新组合,从而 ...

  7. Fiddler替换HTTP Request Host

    原文链接:http://caibaojian.com/fiddler.html 这边指的替换HTTP Request Host是,所有原先发到a.com的HTTP Request , Fiddler都 ...

  8. js parsefloat

    项目中需要对返回的小数进行格式化,把零省略掉. 1.00   ---> 1 1.01   ---> 1.01 1.10   ---> 1.1 parseFloat() 函数可解析一个 ...

  9. Nuget Command Console

    Get-Package -ListAvailable -Filter contracts 命令行 注意选择 程序包源

  10. Linux 命令

    Linux 常用命令 su root  切换root用户 touch /etc/www/html/1.txt  创建文件 mkdir /usr/local/apache2   建立文件夹 rm -rf ...