Last active
December 28, 2018 01:16
-
-
Save qiaoxu123/ab6782084120af3a10db5d659d1c41f4 to your computer and use it in GitHub Desktop.
- 第一种方法:使用额外空间实现,hash表记录遍历过的值,从而找到(暴力解法,复杂度高,用时长)
> Runtime: 124 ms, faster than 45.37% ---
- 第二种方法:使用set数组实现,用count计数来找出现次数为0的数
> Runtime: 200 ms, faster than 6.42%
---
- 第三种方法:参考[Discuss](https://leetcode.com/problems/find-all-numbers-disappeared-
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
vector<int> findDisappearedNumbers(vector<int>& nums) { | |
vector<int> array; | |
vector<int> hash; | |
hash.resize(nums.size()+1); | |
for(int i = 0;i < nums.size();++i) | |
hash[nums[i]]++; | |
for(int i = 1;i <= nums.size();++i) | |
if(!hash[i]) | |
array.push_back(i); | |
return array; | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
vector<int> findDisappearedNumbers(vector<int>& nums) { | |
vector<int> array; | |
set<int> temp; | |
for(int i = 0;i < nums.size();++i) | |
temp.insert(nums[i]); | |
for(int i = 1;i <= nums.size();++i) | |
if(!temp.count(i)) | |
array.push_back(i); | |
return array; | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//参考Discuss写的 | |
//复杂度还是很高 Runtime: 128 ms, faster than 44.12% | |
class Solution { | |
public: | |
vector<int> findDisappearedNumbers(vector<int>& nums) { | |
vector<int> array; | |
for(int i = 1;i <= nums.size();++i){ | |
int index = abs(nums[i-1]) - 1; | |
if(nums[index] > 0){ | |
nums[index] = -nums[index]; | |
} | |
} | |
for(int i = 0;i < nums.size();++i){ | |
if(nums[i] > 0) | |
array.push_back(i+1); | |
} | |
return array; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment