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 ...
随机推荐
- 基于jquery的has()方法以及与find()方法以及filter()方法的区别详解
has(selector选择器或DOM元素) 将匹配元素集合根据选择器或DOM元素为条件,检索该条件在每个元素的后代中是否存在,将符合条件的的元素构成新的结果集. 下面举一个例子: <ul& ...
- WPF MultiRangeSlider 简单实现
WPF 多滑块Slider简单实现(MultiRangeSlider) WPF中的MultiRangeSlider,网上有商业的,有开源的,找了几个都不太理想,那就自己写 一个吧,给大家提供点思路. ...
- Quartz Cron表达式 在线生成器
Cron Expressions——Cron 表达式 按顺序依次为 秒(0~59) 分钟(0~59) 小时(0~23) 天(月)(0~31,但是你需要考虑你月的天数) 月(0~11) 天(星期)(1~ ...
- 【原】Spark学习总结-六个专题
最近从hadoop转向Spark了,学了一段时间了,准备写个专题,主要写pySpark的应用,主要计划是: 主题 内容概要 聚类(5.6) 1.几种常用的聚类算法:2.pyspark中聚类算法的应用( ...
- linux解压rar压缩文件
服务器是阿里云的 centos 默认linux是没有安装解压rar,zip的软件的.而网上找到的yum install命令也没有用.而且这个rar并不是免费的,只有40天的试用期...真是坑啊 由于用 ...
- 1128ORDER BY的原理
工作过程中,各种业务需求在访问数据库的时候要求有order by排序.有时候不必要的或者不合理的排序操作很可能导致数据库系统崩溃.如何处理好order by排序呢?本文从原理以及优化层面介绍 ord ...
- Ubuntu 14.04 掛載 網路磁碟 mount internet disk
1.install cifs tool (Common Internet File System) 新增 /etc/apt/apt.conf.d/01proxy 檔案並加入以下字串,即可透過此台機器做 ...
- AOJ DSL_2_D Range Update Query (RUQ)
Range Update Query 数列 A = {a0,a1 ,...,an−1} に対し.次の2つの操作を行うプログラムを作成せよ. update(s,t,x): as,as+1,...,at ...
- gulp同步执行任务
使用插件:gulp-sync,npm install --save-dev gulp-sync 使用方法参考:https://www.npmjs.com/package/gulp-sync 这些插件也 ...
- IntelliJ IDEA 常用设置讲解
说明 IntelliJ IDEA 有很多人性化的设置我们必须单独拿出来讲解,也因为这些人性化的设置让我们这些 IntelliJ IDEA 死忠粉更加死心塌地使用它和分享它. 常用设置 IntelliJ ...