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. iOS学习-创建带下划线的button

    UIButton *tempBtn = [UIButton buttonWithType: UIButtonTypeCustom]; tempBtn.frame = CGRectMake(, , , ...

  2. swfit-学习笔记(表UITableView的简单使用)

    /*使用与Object-C基本类似,只做简单地使用,创建表及其设置数据源和代理*/ import UIKit class ViewController: UIViewController,UITabl ...

  3. [webpack] 配置react+es6开发环境

    写在前面 每次开新项目都要重新安装需要的包,简单记录一下. 以下仅包含最简单的功能: 编译react 编译es6 打包src中入口文件index.js至dist webpack配置react+es6开 ...

  4. 关于我-dinphy简介

    别   名:孜_行 英文名:dinphy QQ交流群:588266650 兴趣爱好:听音乐.打篮球.热衷于诗词文学 专    业:计算机 了    解:windows及Linux.android的基本 ...

  5. 去掉Win7快捷方式小箭头(win10通用)

    我是一个有强迫症的优化控 , 因为近视的缘故 , 喜欢将桌面图标放大 ,  但是win7快捷方式的小箭头 , 确实不好看 . 用win7魔方之类的软件 , 可以解决这个问题 , 但是有时候重启 ,   ...

  6. sass接触

    第一句话就是棒棒的,我爱上了. 看了真阿当的文章:<2016年前端技术观察> http://weibo.com/ttarticle/p/show?id=230940405256054051 ...

  7. JavaScript数据存取的性能问题

    JavaScript中四种基本的数据存取位置: 字面量:只代表自身 字符串.数字.布尔值.对象.函数.数组.正则,以及null和undefined    快 本地变量:var定义的    快 数组元素 ...

  8. #pragma once与#ifndef #define ...#endif的区别

    1. #pragma once用来防止某个头文件被多次include: #ifndef,#define,#endif用来防止某个宏被多次定义.   2. #pragma once是编译相关,就是说这个 ...

  9. 3MyBatis配置--深入浅出MyBatis技术原理与实践(笔记)

    XML 映射配置文件 configuration 配置 properties 属性 settings 设置 typeAliases 类型命名 typeHandlers 类型处理器 objectFact ...

  10. ClassNotFoundException: org.apache.catalina.loader.DevLoader 自己摸索,丰衣足食

    使用tomcat插件时遇到的问题: ClassNotFoundException: org.apache.catalina.loader.DevLoader 解决方案: 参考了许多文章,对我自己的目录 ...