My first reaction is to have an unlimited length of bit-array, to mark existence. But if no extra mem is allowed, we can simply use 'sign' on each index slot to mark the same info.. it is a common technique.

  1. class Solution {
  2. public:
  3. vector<int> findDisappearedNumbers(vector<int>& nums) {
  4. vector<int> ret;
  5. int n = nums.size();
  6. if(!n) return ret;
  7.  
  8. // Pass 1 - use sign to mark existence.
  9. for(int i = ; i < n; i ++)
  10. {
  11. int inx = abs(nums[i]) - ;
  12. nums[inx] = -abs(nums[inx]);
  13. }
  14.  
  15. // Pass 1 - get all positive
  16. for(int i = ; i < n; i ++)
  17. if(nums[i] > )
  18. ret.push_back(i + );
  19.  
  20. return ret;
  21. }
  22. };

LeetCode "448. Find All Numbers Disappeared in an Array"的更多相关文章

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

  2. leetcode 448. Find All Numbers Disappeared in an Array -easy (重要)

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

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

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

  5. LeetCode: 448 Find All Numbers Disappeared in an Array(easy)

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

  6. LeetCode 448. Find All Numbers Disappeared in an Array找到所有数组中消失的元素

    题目 给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次. 找到所有在 [1, n] 范围之间没有出现在数组中的数字. 您能 ...

  7. [LeetCode] 448. Find All Numbers Disappeared in an Array 找到数组中消失的数字

    题目描述 给定n个数字的数组,里面的值都是1-n,但是有的出现了两遍,因此有的没有出现,求没有出现值这个数组中的值有哪些. 要求不能用额外的空间(除了返回列表之外),时间复杂度n 思路 因为不能用额外 ...

  8. 【leetcode】448. Find All Numbers Disappeared in an Array

    problem 448. Find All Numbers Disappeared in an Array solution: class Solution { public: vector<i ...

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

随机推荐

  1. 微信js-sdk注意事项

    1.录音结束后播放需要localId,用 var voice = { localId: '', serverId: '' }; 来存储,然后用voice.localId引用 2.token和ticke ...

  2. jqGrid几个需要注意的默认设置

    jqGrid中的option属性设置中,几乎每个属性都会给出默认值,然而,有些默认值实在有些坑爹,导致在使用过程中,如果没有注意到而使用了默认值,就会出现一些问题. height:是指jqGrid表格 ...

  3. 认识DOM和一些方法

    认识DOM 文档对象模型DOM(Document Object Model)定义访问和处理HTML文档的标准方法.DOM 将HTML文档呈现为带有元素.属性和文本的树结构(节点树). 先来看看下面代码 ...

  4. SharePoint 2013 图文开发系列之WebPart

    这是我们介绍SharePoint开发入门的第一篇,在这一篇里,我们会介绍SharePoint开发的几个关键物理路径,一些开发技巧和最基础的WebPart开发. 开发工具 在SharePoint 201 ...

  5. Linux 客户端访问 NFS报Permission Denied错误

    在Linux服务器上访问NFS共享目录时,报错:Permission denied. 如下截图所示: 因为这个NFS是系统管理员配置的,我又不了解具体情况,而系统管理员休假中,联系不上.那么我只能先多 ...

  6. SQL Server 2008 R2 升级到 Service Pack 3后Report Builder启动不了

    一同事将测试服务器从SQL Server 2008 R2 SP2升级到了SQL Server 2008 R2 SP3后发现Report Service的报表编辑时启动不了Report Builder, ...

  7. [译文]通过ID, TagName, ClassName, Name, CSS selector 得到element

    致谢原文: <http://xahlee.info/js/js_get_elements.html> 通过ID得到element: Document.getElementById(id s ...

  8. SQL闲杂知识点汇总【2015年12月】

    2015.12.14 知识点1:DEFAULT VALUES实现插入行 --临时创建临时表 CREATE TABLE [dbo].[tblTmp] ( iTmpID ,) NOT NULL PRIMA ...

  9. 配置tomcat https

    生成安全证书 打开命令窗口 Windows键+r,输入cmd 切换当前目录到tomcat的conf目录下 生成证书 红色字体标识的参数按实际需要修改 keytool -genkey -alias to ...

  10. 内存管理内幕mallco及free函数实现

    原文:https://www.ibm.com/developerworks/cn/linux/l-memory/ 为什么必须管理内存 内存管理是计算机编程最为基本的领域之一.在很多脚本语言中,您不必担 ...