268. 丢失的数字 - 力扣(LeetCode) (leetcode-cn.com)
class Solution {
public:
int missingNumber(vector<int>& nums) {
int n=nums.size();
int res;
bool flag=false;
sort(nums.begin(),nums.end());
for(int i=0;i<n;i++)
if(nums[i]!=i) return i;
return n;
}
};
巧妙异或:
class Solution {
public:
int missingNumber(vector<int>& nums) {
int res=nums.size();
for(int i=0;i<nums.size();i++) {
res^=nums[i];
res^=i;
}
return res;
}
};