1、时间复杂度(O(n)),空间复杂度(O(n))
class Solution {
public:
int missingNumber(vector<int>& nums) {
vector<bool> record(nums.size(),true);
for(int i=0;i<nums.size();++i)
if(nums[i]<record.size()) record[nums[i]] = false;
for(int k=0;k<record.size();++k)
if(record[k])return k;
return record.size();
}
};
2、时间复杂度(O(n)),空间复杂度(O(1))
class Solution {
public:
int missingNumber(vector<int>& nums) {
int result = 0 ;
for(int i=0;i<nums.size();++i)
result ^= (i+1) ^ nums[i];
return result;
}
};
3、时间复杂度(O(n)),空间复杂度(O(1))
class Solution {
public:
int missingNumber(vector<int>& nums) {
long n = nums.size();
return n * (n+1) / 2 - accumulate(begin(nums), end(nums), 0);
}
};