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 ...
随机推荐
- KDotAlert
一个iPhone X的适配让楼主受尽了自定义的苦,使用系统API多好,所以在楼主不懈的努力下,终于和组长达成一致:逐步用系统控件替换代码里面的自定义控件,第一个挨刀的就是 BlockAlertsAnd ...
- Codeforces Round #422 (Div. 2)D. My pretty girl Noora(递推+数论)
传送门 题意 对于n个女孩,每次分成x人/组,每组比较次数为\(\frac{x(x+1)}{2}\),直到剩余1人 计算\[\sum_{i=l}^{r}t^{i-l}f(i)\],其中f(i)代表i个 ...
- 退出ACM?
我不知道为什么我有这样的想法,纵观CCCC,太弱太弱,再不把自己埋起来,狠起来,就真的非常菜了,去刷难题吧!我不管老郭的数据,只管自己的实力,每天三道难题CF的C题+总结.以及刷水题!!!(刷CF的B ...
- 鸟哥私房菜基础篇:vim 程序编辑器习题
猫宁!!! 参考链接:http://cn.linux.vbird.org/linux_basic/0310vi.php 鸟哥是为中国信息技术发展做出巨大贡献的人. 1-我用 vi 开启某个档案后,要在 ...
- (六)SpringBoot整合Swagger2框架
一:什么是Swagger Swagger是一款通过我们添加的注解来对方法进行说明,来自动生成项目的在线api接口文档的web服务. 二:添加Swagger2依赖 <dependency> ...
- the little schemer 笔记(2)
第二章 Do it, Do it Again, and Again, and Again... 假设l是 (Jack Sprat could eat no chicken fat) 那么 (lat? ...
- C++命名空间详解
使用命名空间的目的是对标识符的名称进行本地化,以避免命名冲突.在C++中,变量.函数和类都是大量存在的.如果没有命名空间,这些变量.函数.类的名称将都存在于全局命名空间中,会导致很多冲突.比如,如果我 ...
- Codeforces 1107E(区间dp)
用solve(l, r, prefix)代表区间l开始r结束.带了prefix个前缀str[l](即l前面的串化简完压缩成prefix-1个str[l],加上str[l]共有prefix个)的最大值. ...
- 洛谷 P2398 GCD SUM || uva11417,uva11426,uva11424,洛谷P1390,洛谷P2257,洛谷P2568
https://www.luogu.org/problemnew/show/P2398 $原式=\sum_{k=1}^n(k\sum_{i=1}^n\sum_{j=1}^n[(i,j)=k])$ 方法 ...
- jquery选择器 选择除当前点击元素外所有的元素
问题: 多个select选择,如果已选择某些value ,该value不可再选 思路: 点击当前元素,js列出除当前元素外所有的元素 当前解决办法: function symbolDefine(ob ...