原题链接:Leetcode 268. 丢失的数字
方法1:
class Solution {
public:int missingNumber(vector<int>& nums) {sort(nums.begin(),nums.end());int n=nums.size();for(int i=0;i<n;i++){if(nums[i]!=i) return i;}return n;}
};
位运算:
class Solution {
public:int missingNumber(vector<int>& nums) {int n=nums.size()+1;for(int i=0;i<n;i++){nums.push_back(i);}int res=0;for(auto x:nums){res^=x;}return res;}
};