[LeetCode]448. Find All Numbers Disappeared in an Array
来源:互联网 发布:局域网教学软件 编辑:程序博客网 时间:2024/06/07 05:12
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]
跟之前那道Find the Duplicate Number极其类似,那道题让找出所有重复的数字,这道题让找不存在的数,这类问题的一个重要条件就是1 ≤ a[i] ≤ n (n = size of array),不然很难在O(1)空间和O(n)时间内完成。
第一种解法,这种解法的思路路是,对于每个数字nums[i],如果其对应的nums[nums[i] - 1]是正数,我们就赋值为其相反数,如果已经是负数了,就不变了,那么最后我们只要把留下的整数对应的位置加入结果res中即可.
public class Solution { public List<Integer> findDisappearedNumbers(int[] nums) { List<Integer> list = new LinkedList<Integer>(); for(int i=0; i<nums.length; i++){ if(nums[Math.abs(nums[i])-1]>0) { nums[Math.abs(nums[i])-1]*=-1; } } for(int i=0; i<nums.length; i++){ if(nums[i]>0) list.add(i+1); } return list; }}
阅读全文
0 0
- LeetCode-Array-448. Find All Numbers Disappeared in an Array
- leetcode:448. Find All Numbers Disappeared in an Array
- LeetCode 448. Find All Numbers Disappeared in an Array
- <leetcode>448.Find All Numbers Disappeared in an Array
- [LeetCode] 448. Find All Numbers Disappeared in an Array
- [LeetCode]448. Find All Numbers Disappeared in an Array
- leetcode 448. Find All Numbers Disappeared in an Array
- Leetcode 448. Find All Numbers Disappeared in an Array
- 【LeetCode】 448. Find All Numbers Disappeared in an Array
- 448. [LeetCode]Find All Numbers Disappeared in an Array
- [leetcode] 448. Find All Numbers Disappeared in an Array
- LeetCode 448. Find All Numbers Disappeared in an Array
- Leetcode-448. Find All Numbers Disappeared in an Array
- 【Leetcode】448. Find All Numbers Disappeared in an Array
- LeetCode - 448. Find All Numbers Disappeared in an Array
- LeetCode 448. Find All Numbers Disappeared in an Array
- LeetCode--448. Find All Numbers Disappeared in an Array
- LeetCode 448. Find All Numbers Disappeared in an Array(Java)
- LLVM-4.0.1 linux配置安装(64位Ubuntu16)
- json范例1
- opencv实现camshift算法,以及代码详解
- Centos Yum安装Chrome浏览器
- [展览人周刊]华展云20170731期
- [LeetCode]448. Find All Numbers Disappeared in an Array
- 远程连接Windows服务器 【由于没有远程桌面授权服务器可以提供许可证,远程会话被中断。】mstsc /admin
- TCP->多进程服务器->多进程服务器->线程池
- cf The Festive Evening(水)
- LinkedList源码分析
- itext对水印图片的旋转、放大、缩小等操作的示例
- oralce查询多列数据库操作
- DOS下编译运行小应用程序
- Linux下Android Studio中的AVD模拟器启动无反应解决方法