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 ...
随机推荐
- [CareerCup] 6.6 Toggle Lockers 切换锁的状态
6.6 There are 100 closed lockers in a hallway. A man begins by opening all 100 lockers. Next, he clo ...
- css点滴
1.vertical-align这个属性用于块元素的垂直,居中,行元素用line-height. text-align比如li span时,text-align用于上一句的li的元素时,span才会居 ...
- mysql5.7.10 的源码安装
mysql 5.7.10的源码安装:http://fyduan.blog.51cto.com/4234935/1729873cmake . -DCMAKE_INSTALL_PREFIX=/usr/lo ...
- javaweb局部刷新-ajax异步请求springMVC显示返回的jsp内容,代替iframe
在jsp上要引入jquery <script src="<%=request.getContextPath()%>/js/jquery_ui/jquery.js" ...
- 8 HTML&JS等前端知识系列之Ajax的例子
what is ajax ? AJAX = 异步 JavaScript 和 XML. AJAX 是一种用于创建快速动态网页的技术. 通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新 ...
- LogStash配置、使用(三)
LogStash配置 官方文档:https://www.elastic.co/guide/en/logstash/current/index.html 查看yum安装路径 rpm -ql logsta ...
- 基于 Arduino 的 RFID 识别实验
http://www.it165.net/embed/html/201512/3287.html 2015年12月04日(周五) 上午 博士的智能卡实验--RFID识别实验,基于51单片机: 我们的 ...
- Jquery的 each的使用 $.each()
下面提一下each的几种常用的用法 1.each处理一维数组 var arr1 = [ "aaa", "bbb", "ccc" ]; $.e ...
- wpf 获取datagrid中模板中控件
//获取name为datagrid中第三列第一行模板的控件 FrameworkElement item = dataGrid.Columns[].GetCellContent(dataGrid.Ite ...
- Python Day5
模块 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个 ...