LEETCODE-- Find All Numbers Disappeared in an Array
2017-01-31 11:31
561 查看
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]
这里使用到正负号标记法转载
大致思想:nums中元素值当作nums的下标进行操作,即凡是与nums中含有的元素值相同的下标下对应的元素变成负数(这里要注意如果前面的元素对后边的元素进行了改变,后边的元素再进行相应计算时有可能已经被变为负数,所以要在对元素进行计算时进行绝对值计算abs();)
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]
这里使用到正负号标记法转载
大致思想:nums中元素值当作nums的下标进行操作,即凡是与nums中含有的元素值相同的下标下对应的元素变成负数(这里要注意如果前面的元素对后边的元素进行了改变,后边的元素再进行相应计算时有可能已经被变为负数,所以要在对元素进行计算时进行绝对值计算abs();)
class Solution { public: vector<int> findDisappearedNumbers(vector<int>& nums) { int len = nums.size(); for(int i = 0; i < len; i++){ cout << nums[i] << endl; int n = abs(nums[i]) - 1; nums = -abs(nums ); } vector<int> dis; for(int j = 0; j < len; j++){ if(nums[j] > 0) dis.push_back(j + 1); } return dis; } };
相关文章推荐
- Android弹出框详解
- Android 异步消息处理机制 让你深入理解 Looper、Handler、Message三者关系
- android_108_PullToRefresh
- Android中事件处理机制之——ViewGroup的事件传递详解(一)
- Cookie、Session 与 application
- android_107_ProgressBar旋转
- 微信小程序的报价预算
- Android文件读写和使用SharedPreferences储存数据
- Swift中去调用FMDB
- iOS开发-SQLite数据库在App项目中的设置及使用
- 在 CollapsingToolbarLayout 的基础上实现的另一种折叠效果
- 视频H5のVideo标签在微信里的坑和技巧
- swift教程
- 怎么实现类似"今日头条"app
- Objective-C Block
- hibernate中出现 文档根元素 "hibernate-mapping" 必须匹配 DOCTYPE 根 "hibernate-configuration"
- 浅析 UIApplication 生命周期的一些 delegate 方法
- Android SDK Sample(一) CardReader
- 微信小程序支付失败 签名失败及签名错误的解析 -- KeeSoft框架
- Delphi使用android的NDK是通过JNI接口,封装好了,不用自己写本地代码,直接调用