Runtime: 124 ms, faster than 45.37%
Runtime: 200 ms, faster than 6.42%
复杂度也很高。。
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;
}
};
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;
}
};
//参考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;
}
};