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 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]
public class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> list = new ArrayList<Integer>();
if (nums == null || nums.length == ) return list;
for (int i = ; i < nums.length; i++) {
int val = nums[i];
if (nums[i] != -) {
nums[i] = -;
exchange(nums, val);
}
}
for (int i = ; i < nums.length; i++) {
if (nums[i] == -) {
list.add(i + );
}
}
return list;
}
public void exchange(int[] nums, int val) {
if (nums[val - ] == val) return;
int temp = nums[val - ];
nums[val - ] = val;
if (temp != -) {
exchange(nums, temp);
}
}
}
public class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> list = new ArrayList<Integer>();
if (nums == null || nums.length == )
return list;
for (int i = ; i < nums.length; i++) {
int index = Math.abs(nums[i]) - ;
if (nums[index] > ) {
nums[index] = -nums[index];
}
}
for (int i = ; i < nums.length; i++) {
if (nums[i] > ) {
list.add(i + );
}
}
return list;
}
}
Find All Numbers Disappeared in an Array的更多相关文章
- leetcode之Find All Numbers Disappeared in an Array
问题来源:Find All Numbers Disappeared in an Array 很久没有刷题了,感觉大脑开始迟钝,所以决定重拾刷题的乐趣.一开始不要太难,选一些通过率高的题目做,然后就看到 ...
- 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 = ...
- 【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 ...
- LeetCode——Find All Numbers Disappeared in an Array
LeetCode--Find All Numbers Disappeared in an Array Question Given an array of integers where 1 ≤ a[i ...
- 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 ...
- 448. Find All Numbers Disappeared in an Array@python
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
448. Find All Numbers Disappeared in an Array Easy Given an array of integers where 1 ≤ a[i] ≤ n (n ...
- LeetCode Find All Numbers Disappeared in an Array
原题链接在这里:https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/ 题目: Given an array o ...
- [LeetCode] 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 ...
随机推荐
- PL/SQL 如何查看当前连接信息以及SQL PLUS如何指定IP地址
1.通过PL/SQL 想连接别的数据库可以通过在服务名前面加上IP和左斜线来实现: 2.有时我们的IP和左斜线不用输也能连别的数据库,是因为在一个文件里配置好了.这个文件在哪里? 在这个路径下的 NE ...
- Excel到底最多可以有多少行
平时我们使用的Excel到底最多可以有多少行? 答案:1048576,如图在红框中输入此数字即可看见.
- 一种全新的屏幕适配方法 自动百分比适配 一切px说了算
看hongyang的博客 发现他的一个按百分比适配的方案 : 库地址https://github.com/hongyangAndroid/AndroidAutoLayout 博客地址http://bl ...
- Js前端代码异常监控
window.onerror = function(msg,url,line,col,error){ //没有URL不上报!上报也不知道错误 if (msg != "Script error ...
- 微信支付开发(1) JS API支付
关键字:微信支付 微信支付v3 jsapi支付 统一支付 Native支付 prepay_id 作者:方倍工作室原文: http://www.cnblogs.com/txw1958/p/wxpayv3 ...
- JQuery中Ajax的操作
JQuery Ajax异步操作的方式: $.ajax,$.post, $.get, $.getJSON. 一, $.ajax,这个是JQuery对ajax封装的最基础步,通过使用这个函数可以完成异 ...
- 转发 VS 重定向
转发:JSP容器将使用一个内部的方法来调用目标页面,新的页面继续处理同一个请求,而浏览器将不会知道这个过程.以前的request中存放的变量全部失效,并进入一个新的request作用域. 重定向:第一 ...
- 【UOJ #13】【UER #1】跳蚤OS
http://uoj.ac/problem/13 建立trie树,然后建立go指针, 和AC自动机里的fail指针差不多, 走到一个快捷方式就从go指针走. 注意在trie树上要保留字符'/',不能用 ...
- jQuery给动态添加的元素绑定事件的方法
我们在开发过程会遇到无法给动态元素添加绑定事件,解决方案如下: 例如 <div id="testdiv"> <ul></ul> </d ...
- Hbase集群master.HMasterCommandLine: Master exiting
2016-12-15 17:01:57,473 INFO [main] impl.MetricsSystemImpl: HBase metrics system started 2016-12-15 ...