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 ...
随机推荐
- IO(六)--- 编码和解码
编码: 把看得懂的字符变成看不懂码值这个过程我们称作为编码. 解码: 把码值查找对应的字符,我们把这个过程称作为解码. 注意: 以后编码与解码一般我们都使用统一的码表.否则非常容易出乱码. 常用码表: ...
- ReactNative新手学习之路05 使用夜神模拟器调试ReactNative
1.首先确保adb环境添加到path环境 2.安装好夜神模拟器 3.运行模拟器 4.adb connect 127.0.0.1:62001 5.摇一摇设置IP和端口 如127.168. ...
- LeetCode:Max Points on a Line
题目链接 Given n points on a 2D plane, find the maximum number of points that lie on the same straight l ...
- 如何查看SQL Server的版本、补丁包信息?以及如何鉴别是否需要升级自己的SQL Server?
作为一个SQL Server的数据库管理人员,经常需要碰到的一个问题就是查看自己SQL Server属于哪个版本,是否安装了最新的修复补丁包,是否安装了最近的安全补丁.在此之前,我们可以通过以下SQL ...
- JavaScript的理解记录(6)
---接上篇: 四.CSS相关: 1.CSS不支持注释// 支持注释/* */ 2. 几种浏览器厂商前缀: Firefox : -moz-; Chrome:-webkit- ; IE: ...
- resin启动报错:guava-15.0.jar!/META-INF/beans.xml:5: <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"> is an unexpected top-level tag. 异常
项目完成,经过本地的测试,最后在部署的时候,发现服务器resin启动失败,报错信息如下:
- OpenCV图像细化的一个例子
转自:http://blog.csdn.net/zfdxx369/article/details/9091953?utm_source=tuicool 本文是zhang的一篇经典图像细化论文,效果很好 ...
- 最简单的JavaScript模板引擎
在小公司待久了感觉自己的知识面很小,最近逛博客园和一些技术网站看大家在说JavaScript模版引擎的事儿,完全没有概念,网上一搜这是08年开始流行起来的...本来以为这是很高深的知识,后来在网上看到 ...
- JQuery 实现标题与内容相呼应样式
效果图如下:鼠标移到标题上,标题下面显示标题对应的内容.实现的方法如下: 1.引入css和js文件 <link href="public/CSS/StyleSheet.css" ...
- poj 1390 Blocks
poj 1390 Blocks 题意 一排带有颜色的砖块,每一个可以消除相同颜色的砖块,,每一次可以到块数k的平方分数.问怎么消能使分数最大.. 题解 此题在徐源盛<对一类动态规划问题的研究&g ...