136. Single Number

Given an array of integers, every element appears twice except for one. Find that single one.

异或操作

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


你可能感兴趣的:(136. Single Number)