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 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.
Example:
Input:
[4,3,2,7,8,2,3,1] Output:
[5,6]
原题地址: Find All Numbers Disappeared in an Array
难度: Easy
题意: 给出n个数,范围在[1, n]之间,数组中存在重复,返回1-n之中缺少的数,要求不用额外的空间,时间复杂度为O(n)
不用额外的空间,只能用数组进行计数
(1)遍历数组,用索引进行计数,出现一次将索引值变为负数.数值[1, n]对应的索引为[0, n-1]比如上个例子中4,索引值为3,所以将数值7变为-7
(2)遍历一次之后,重复一次的值的索引对应的值为正数,而其他数都为负数, 再遍历一次,找出值为正数的索引值
代码:
class Solution(object):
def findDisappearedNumbers(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
n = len(nums)
res = []
for i in range(n):
idx = abs(nums[i]) - 1 # 索引值
nums[idx] = -abs(nums[idx]) for i in range(n):
if nums[i] > 0:
res.append(i+1)
return res
时间复杂度:O(n)
空间复杂度:O(1)
448. Find All Numbers Disappeared in an Array@python的更多相关文章
- 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 ...
 - 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 ...
 
随机推荐
- mysql数据库基本操作(五)
			
表纪录之查(单表查询)2 上一篇讲了4类查询的方式,现在接着上一篇继续看还有哪些方式. group by 分组查询 顾名思义,分组查询要分组,因为mysql5.7之后默认不支持group by语句,需 ...
 - codeforces 611C
			
题意: 给你一个矩阵,矩阵里有" . "和" # "," . "表示空的," # "表示禁止的. 多米诺骨牌将占据正好有 ...
 - Luogu P1850换教室【期望dp】By cellur925
			
题目传送门 首先这个题我们一看它就是和概率期望有关,而大多数时候在OI中遇到他们时,都是与dp相关的. \(Vergil\)学长表示,作为\(NOIp2016\)的当事人,他们考前奶联赛一定不会考概率 ...
 - LuoguP2055 [ZJOI2009]假期的宿舍【二分图最大匹配】By cellur925
			
题目传送门 这道题开始感觉不出是二分图最大匹配的qwq.但是今天学了匈牙利算法,想来做几个题qwq.做这个题的时候想了很久它哪里是二分图,脑子里是“两列,每列有很多点的那种图 qwq.” 然后看了题解 ...
 - Eclipse - Maven项目Update Project后jdk版本变成1.5
			
问题与分析 最近遇到个奇怪的问题,在Eclipse里对一个Maven项目进行Update Project(快捷键是 Alt+F5),原本jdk为1.8的项目忽然就变成了1.5,于是就报了一些错误. 我 ...
 - Centos 6.x 搭建 Zabbix Agent 客户端
			
如需搭建zabbix server端,请参考:Zabbix-Server配置 环境: Zabbix-Server: Centos 6.8 IP:192.168.126.129 #Zabix- ...
 - Java图解
			
java虚拟机 JVM运行过程: java开发工具包 java入门图解1 java入门图解2 java入门图解3 java入门图解4
 - [ AHOI 2013 ] 作业 & [ BZOJ 3809 ] Gty的二逼妹子序列
			
\(\\\) Description 给出一个长为 \(n\) 的数列 \(A\) 和 \(k\),多次询问: 对于一个区间 \([L_i,R_i]\),问区间内有多少个数在 \([a_i,b_i]\ ...
 - CF985D Sand Fortress
			
思路: 很奇怪的结论题,不好想.参考了http://codeforces.com/blog/entry/59623 实现: #include <bits/stdc++.h> using n ...
 - 响应式布局 max-device-width 与 max-width 的区别
			
闲来没事,研究了一下多屏适配和响应式布局的 CSS. 第一种写法 @media screen and (max-device-width: 320px) { } @media screen and ( ...