leetcode136. 只出现一次的数字

暴力

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        sort(nums.begin(),nums.end());
        int i=0;
        while(i<nums.size()){
            if(i+1<nums.size()&&nums[i]==nums[i+1])i+=2;
            else break;
        }
        return nums[i];
    }
};

位运算

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int ans=0;
        for(auto t:nums){
            ans^=t;
        }
        return ans;
    }
};