C++ solution 16ms

  • 2
    vector<int> singleNumber(vector<int>& nums) {
        int n=nums.size();
        int res=0;
        int a=0,b=0;
        for(int i=0;i<n;i++){
            res ^= nums[i];
        int cmp=res&(res-1)^res;    //按a b 最后一个不同位分两组
        for(int i=0;i<n;i++){
            if(cmp & nums[i])
                a ^= nums[i];
                b ^= nums[i];
        return vector<int>{a,b};

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.