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. java 发送get,post请求

    package wzh.Http; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStr ...

  2. Unity Transform常识(转)

    Variables   position: Vector3  物体在世界坐标中的位置. transform.position=Vector3(10,10,10)//把物体放到(x=10,y=10,z= ...

  3. python __builtins__ classmethod类 (11)

    11.'classmethod', 修饰符对应的函数不需要实例化,不需要 self 参数,但第一个参数需要是表示自身类的 cls 参数,可以来调用类的属性,类的方法,实例化对象等. class cla ...

  4. jQuery笔记之工具方法—高级方法Ajax

    $.ajxa() ——基本使用 前提:先了解js的执行机制 $.Callbacks()——回调 $.Deferred()——异步 $.when() 网络服务器链接由<渡一教育>提供 --- ...

  5. 易爆物(X-Plosives )基础并查集

    #include <iostream> #include <algorithm> using namespace std; + ; int fa[maxn]; int Find ...

  6. Markdown 简单使用教程

    标题: # 一级标题 ## 二级标题 增加星号,字号相应变小,共有6级 列表: - 无序列表 编号.  有序列表 插入链接: [显示文本](链接地址) 插入图片: ![](图片地址) 引用: > ...

  7. Subsequence HDU - 3530

    Subsequence HDU - 3530 方法:单调队列区间最大最小 错误记录(本地写错)的原因:写成每次试着扩展右端点,却难以正确地处理"在多扩展右端点之后减去多扩展的部分" ...

  8. Party Games UVA - 1610

    题目 #include<iostream> #include<string> #include<algorithm> using namespace std; // ...

  9. python tkinter窗口弹出置顶的方法

    加上下面两句即可实现root窗口的置顶显示,可以用于某些程序的消息提示,能够弹出到桌面显示 root = Tk() root.wm_attributes('-topmost',1)

  10. Mysql中的索引问题

    索引的用途 提高查询的效率,相当于在字典中建立的字母表或者偏旁部首表,这样查询当然比一行一行查询要快的多 每个存储引擎可以建立索引的长度是不一样的,但每个表至少支持16个索引,总的索引长度至少为256 ...