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的更多相关文章

  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. 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 ...

  6. 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 -easy (重要)

    题目链接: https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/ 题目描述: Give ...

  8. 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 ...

  9. [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. CodeForces 712C Memory and De-Evolution (贪心+暴力)

    题意:现在有一个长度为 x 的正三角形,每次可以把一条边减小,然后用最少的时间变成长度为 y 的正三角形. 析:一开始,正着想,然后有一个问题,就是第一次减小多少才能最快呢?这个好像并不好确定,然后我 ...

  2. 模板 - 字符串 - Manacher

    求最长回文子串. #include<bits/stdc++.h> using namespace std; #define ll long long ; ]; ]; int Manache ...

  3. 基于FBX SDK的FBX模型解析与加载 -(四)

    8. 骨骼蒙皮动画 骨骼蒙皮动画是当前游戏引擎中最常用的一种动画方式,关于其基本原理网络上的资料较多,关于到涉及的其它较复杂操作,如插值.融合等在这里也就先不再讨论了,而且其实现方式也与具体引擎的动作 ...

  4. Cg(C for Graphic)语言语义绑定方法(转)

    摘抄“GPU Programming And Cg Language Primer 1rd Edition” 中文名“GPU编程与CG语言之阳春白雪下里巴人” 语义绑定方法 入口函数输入\ 输出数据的 ...

  5. bzoj 4316: 小C的独立集【仙人掌dp】

    参考:https://www.cnblogs.com/clrs97/p/7518696.html 其实和圆方树没什么关系 设f[i][j][k]为i点选/不选,这个环的底选不选 这个底的定义是设u为这 ...

  6. .NET Core 跨平台物联网开发:设置委托事件(二)

    系列教程目录 (一) 连接阿里云IOT (二) 设置委托事件 (三) 上报属性 (四)  SDK文档 属性.方法.委托.类 http://pan.whuanle.cn/index.php?dir=up ...

  7. ubuntu 18 安装virtulenv以及virtualenvwrapper

    转自: https://www.jianshu.com/p/06533f19c4ad 首先安装pip,如果用的python3版本要安装pip3(自行google) sudo apt install v ...

  8. PostgreSQL - 修改默认端口号

    升级PostgreSQL遇到的问题 之前将PostgreSQL从9.5升级到了10.3版本,安装时将端口设置成了5433,(默认是5432),后来发现在使用psql来restore db会发生语法错误 ...

  9. jquery html() 和text()的用法

    html()类似JS中的 innerHTML,首先看一段代码: <!DOCTYPE html> <html lang="en"> <head> ...

  10. 洛谷P2502[HAOI2006]旅行

    题目: Z小镇是一个景色宜人的地方,吸引来自各地的观光客来此旅游观光.Z小镇附近共有N个景点(编号为1,2,3,-,N),这些景点被M条道路连接着,所有道路都是双向的,两个景点之间可能有多条道路.也许 ...