LeetCode "448. Find All Numbers Disappeared in an Array"
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.
- class Solution {
- public:
- vector<int> findDisappearedNumbers(vector<int>& nums) {
- vector<int> ret;
- int n = nums.size();
- if(!n) return ret;
- // Pass 1 - use sign to mark existence.
- for(int i = ; i < n; i ++)
- {
- int inx = abs(nums[i]) - ;
- nums[inx] = -abs(nums[inx]);
- }
- // Pass 1 - get all positive
- for(int i = ; i < n; i ++)
- if(nums[i] > )
- ret.push_back(i + );
- return ret;
- }
- };
LeetCode "448. Find All Numbers Disappeared in an Array"的更多相关文章
- 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 ...
- 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 a ...
- 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 ...
- LeetCode 448. Find All Numbers Disappeared in an Array找到所有数组中消失的元素
题目 给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次. 找到所有在 [1, n] 范围之间没有出现在数组中的数字. 您能 ...
- [LeetCode] 448. Find All Numbers Disappeared in an Array 找到数组中消失的数字
题目描述 给定n个数字的数组,里面的值都是1-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 ...
随机推荐
- 微信js-sdk注意事项
1.录音结束后播放需要localId,用 var voice = { localId: '', serverId: '' }; 来存储,然后用voice.localId引用 2.token和ticke ...
- jqGrid几个需要注意的默认设置
jqGrid中的option属性设置中,几乎每个属性都会给出默认值,然而,有些默认值实在有些坑爹,导致在使用过程中,如果没有注意到而使用了默认值,就会出现一些问题. height:是指jqGrid表格 ...
- 认识DOM和一些方法
认识DOM 文档对象模型DOM(Document Object Model)定义访问和处理HTML文档的标准方法.DOM 将HTML文档呈现为带有元素.属性和文本的树结构(节点树). 先来看看下面代码 ...
- SharePoint 2013 图文开发系列之WebPart
这是我们介绍SharePoint开发入门的第一篇,在这一篇里,我们会介绍SharePoint开发的几个关键物理路径,一些开发技巧和最基础的WebPart开发. 开发工具 在SharePoint 201 ...
- Linux 客户端访问 NFS报Permission Denied错误
在Linux服务器上访问NFS共享目录时,报错:Permission denied. 如下截图所示: 因为这个NFS是系统管理员配置的,我又不了解具体情况,而系统管理员休假中,联系不上.那么我只能先多 ...
- SQL Server 2008 R2 升级到 Service Pack 3后Report Builder启动不了
一同事将测试服务器从SQL Server 2008 R2 SP2升级到了SQL Server 2008 R2 SP3后发现Report Service的报表编辑时启动不了Report Builder, ...
- [译文]通过ID, TagName, ClassName, Name, CSS selector 得到element
致谢原文: <http://xahlee.info/js/js_get_elements.html> 通过ID得到element: Document.getElementById(id s ...
- SQL闲杂知识点汇总【2015年12月】
2015.12.14 知识点1:DEFAULT VALUES实现插入行 --临时创建临时表 CREATE TABLE [dbo].[tblTmp] ( iTmpID ,) NOT NULL PRIMA ...
- 配置tomcat https
生成安全证书 打开命令窗口 Windows键+r,输入cmd 切换当前目录到tomcat的conf目录下 生成证书 红色字体标识的参数按实际需要修改 keytool -genkey -alias to ...
- 内存管理内幕mallco及free函数实现
原文:https://www.ibm.com/developerworks/cn/linux/l-memory/ 为什么必须管理内存 内存管理是计算机编程最为基本的领域之一.在很多脚本语言中,您不必担 ...