448. Find All Numbers Disappeared in an Array
来源:互联网 发布:python 打印日志 编辑:程序博客网 时间:2024/06/05 19:05
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]
我们把所有出现过的数的位置设为负值,最后没有出现过的位置就是正值。用三目运算符保证我们不会覆盖未遍历的值,仅仅将其设置为负值。
class Solution {public: vector<int> findDisappearedNumbers(vector<int>& nums) { for(int i=0; i<nums.size(); ++i){ int m = abs(nums[i]) - 1; nums[m] = nums[m] > 0 ? -nums[m] : nums[m]; } vector<int> res; for(int i=0; i<nums.size(); ++i){ if(nums[i] > 0) res.push_back(i+1); } return res; }};
0 0
- LeetCode-Array-448. Find All Numbers Disappeared in an Array
- [LC][Array] 448. Find All Numbers Disappeared in an Array
- Find All Numbers Disappeared in an Array
- Find All Numbers Disappeared in an Array
- Find All Numbers Disappeared in an Array
- Find All Numbers Disappeared in an Array
- Find All Numbers Disappeared in an Array
- Find All Numbers Disappeared in an Array
- Find All Numbers Disappeared in an Array
- Find All Numbers Disappeared in an Array
- Find All Numbers Disappeared in an Array
- Find All Numbers Disappeared in an Array
- Find All Numbers Disappeared in an Array
- Find All Numbers Disappeared in an Array
- 448. Find All Numbers Disappeared in an Array
- 448. Find All Numbers Disappeared in an Array
- 448. Find All Numbers Disappeared in an Array
- leetcode:448. Find All Numbers Disappeared in an Array
- git push -u origin master 出错 \Permission denied (publickey). fatal: Could not read from remote repo
- Java多线程——Callable、Future和FutureTask
- ViewHolder报空指针????
- 初试 sproto
- ERROR in AppModule is not an NgModule
- 448. Find All Numbers Disappeared in an Array
- 深入浅出RxJava2.0_01[重新理解背压概念]
- Builder模式演义(2)——OkHttp源码中的Builder模式
- js数组中forEach,map,filter的区别
- 求迷宫出路以及最优解,小试牛刀。
- Android中IntentService和HandlerThread的分析
- Spring3.X jdk8 java.lang.IllegalArgumentException
- react-native 学习《一》
- 并发编程---volatile关键字详解及使用