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]
解法:因为要修改输入,个人觉得这并不是一个优雅的解法。
class Solution(object):def findDisappearedNumbers(self, nums):""":type nums: List[int]:rtype: List[int]"""# use nums as a map# if x in nums appear, set nums[x] to negative# if [1,n] in map, map it with negative numberfor n in nums:i = abs(n)-1if nums[i]>0:nums[i] = -nums[i]return [i for i in xrange(1, len(nums)+1) if nums[i-1]>0]                    

解法2:排序,将数值放到对应位置上。使用贪心算法!

class Solution(object):def findDisappearedNumbers(self, nums):""":type nums: List[int]:rtype: List[int]"""# sort num in nums to its right position# for each num, use greedy algo to move num to its right pos, until nums[i] = i+1 # for example:# [4,3,2,7,8,2,3,1]# [7,3,2,4,8,2,3,1]# [3,3,2,4,8,2,7,1]# [2,3,3,4,8,2,7,1]# [3,2,3,4,8,2,7,1]# pos 3 is 3 end# [3,2,3,4,8,2,7,1]# [3,2,3,4,1,2,7,8]# [1,2,3,4,3,2,7,8]# pos 3 is 3 end# ...for i,n in enumerate(nums):while nums[nums[i]-1] != nums[i]:nums[nums[i]-1], nums[i] = nums[i], nums[nums[i]-1]return [i+1 for i,n in enumerate(nums) if n!=i+1]                            

转载于:https://www.cnblogs.com/bonelee/p/8589088.html

leetcode 448. Find All Numbers Disappeared in an Array相关推荐

  1. Leetcode 448. Find All Numbers Disappeared in an Array

    Leetcode  448. Find All Numbers Disappeared in an Array Add to List Description Submission Solutions ...

  2. LeetCode 448. Find All Numbers Disappeared in an Array 442. Find All Duplicates in an Array

    这两道题很有意思,由于元素为1~n,因此每个元素的值-1(映射到0~n-1)就可以直接当做下标.这样将 nums 中对应下标的元素 *-1 以i表示 index+1 这个元素出现过了,能节省存储的空间 ...

  3. leetcode 448. Find All Numbers Disappeared in an Array | 448. 找到所有数组中消失的数字(原地,位运算)

    题目 https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/ 题解 遍历数组,将数组中每个数字 n 作为下标,将 ...

  4. [swift] 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 ...

  5. LeetCode之Find All Numbers Disappeared in an Array

    1.题目 Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice a ...

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

  8. 448. Find All Numbers Disappeared in an Array 寻找有界数组[1,n]中的缺失数

    [抄题]: Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice ...

  9. 448. Find All Numbers Disappeared in an Array645. Set Mismatch

    题目: 448. Find All Numbers Disappeared in an Array Given an array of integers where 1 ≤ a[i] ≤ n (n = ...

最新文章

  1. 在Ubuntu 16.04.3 LTS上玩转QUIC开源项目proto-quic
  2. 2011百度之星初赛B圆环
  3. SpringBoot中使用POI实现自定义Excel布局式导出
  4. 用python turtle库画正方形_用Python Turtle画一个正方形
  5. html捉虫游戏,幼儿园体育游戏《小鸡捉虫》教案(精选4篇)
  6. 问题 I: Sequence Problem : Array Practice
  7. linux建sybase设备,linux sybase 安装全过程(写给初学者)
  8. 物联网毕业设计 超声波雷达可视化系统
  9. java工程师考试要求_java初级工程师考试内容
  10. 互联网数据分析--指标和维度
  11. linux 进程带宽限制,Linux限制网络带宽的占用
  12. cf1009 C. Annoying Present
  13. MyBatis万能插入语句
  14. html5表格在线生成,专业的Web报表工具——表格在线生成制作工具
  15. 2022全国节能宣传周 | 志翔科技大数据:支撑电企降碳、生活节能
  16. 【python种子项目ppc】一行代码生成项目与开发详细指导
  17. SCI科研工具推荐合集!
  18. python与炒股_Python量化炒股入门与实战技巧
  19. 商业周刊:MySpace兴衰沉浮启示录(转)
  20. OpenGL解析法绘制旋转曲面--青花瓷

热门文章

  1. libevent 获取多线程结构体变量加锁方法
  2. python txt提取特定数据_Python提取列表中的内容 用“python”怎么提取文件里的指定内容?...
  3. vb.net 线程偶尔不会自动关闭_C# 多线程系列(7)手动线程通知
  4. ct读片软件_伦琴影领影像诊断中心:这六大MRI读片技巧,影像医生必须掌握
  5. python批量删除文件1001python批量删除文件_Python实现递归遍历文件夹并删除文件...
  6. c语言中仅分号,问什么C程序里总是提示缺少分号;,而明明有分号?
  7. jquery的sortable拖拽排序问题,在页面上多次拖拽保存顺序之后,刷新页面,排序出现紊乱
  8. file_get_contents(php://input)的使用方法
  9. 不同层级的Java开发者的不同行为
  10. Android开发应该了解的Binder原理,专题解析