448. Find All Numbers Disappeared in an Array
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的更多相关文章
- 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 = ...
- 【leetcode】448. Find All Numbers Disappeared in an Array
problem 448. Find All Numbers Disappeared in an Array solution: class Solution { public: vector<i ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- leetcode 448. Find All Numbers Disappeared in an Array -easy (重要)
题目链接: https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/ 题目描述: Give ...
- 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 ...
- [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 ...
随机推荐
- AngularJS入门教程
1. 简介:AngularJS是为了克服HTML在构建应用上的不足而设计的.HTML是一门很好的为静态文本展示设计的声明式语言,但要构建WEB应用的话它就显得乏力了.所以我做了一些工作(你也可以觉得是 ...
- 记录PHP的超全局变量$_SERVER
$_SERVER是PHP中十分实用的超全局变量,在开发可移植的网站的时候会变得很有用. 下面我记录一下我自己常用到的几个变量 1.$_SERVER['SERVER_NAME']:记录了网站的域名. 2 ...
- jqGrid jqGrid分页参数+条件查询
HTML <div class="row"> <div class="col-sm-20"> <form id="for ...
- 递推 hdu 1330
http://www.cnblogs.com/rainydays/archive/2013/01/16/2862235.html 看样例的答案 #include<stdio.h> #inc ...
- 跟着《beginning jquery》学写slider插件并借助自定义事件改进它
<beginning jquery>是一本很不错的学习jquery的书,作者的讲解深入浅出,很适合初学者,在最后一章里面,作者把前面所有的点结合起来完成了一个轮播图的jquery插件.实现 ...
- 架构师养成记--6.单例和多线程、ThreadLocal
一.ThreadLocal 使用wait/notify方式实现的线程安全,性能将受到很大影响.解决方案是用空间换时间,不用锁也能实现线程安全. 来看一个小例子,在线程内的set.get就是thread ...
- JavaScript备忘录
提取字符串substring(start,end)substr(start [, length ])JavaScript substr() 方法 --
- C#基础强化-进程操作
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...
- Django基础,Day7 - 添加静态文件 static files
添加css样式文件 1.首先在app目录下创建static文件夹,如polls/static.django会自动找到放在这里的静态文件. AppDirectoriesFinder which look ...
- SqlServer批量刷数据执行事务回滚语句备份
企业进行对数据库执行刷数据工作,一段很长的语句希望同时成功或者失败时用到. 1.建立测试环境 /**************************************************** ...