Leetcode 260. Digital III appears only once and problem-solving ideas implemented in C ++

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/gjh13/article/details/90734039

Problem-solving ideas:

This question is really a bit iffy, see: https://blog.csdn.net/smile_watermelon/article/details/47750249

First mark, then after careful study.

 

class Solution {
public:
    vector<int> singleNumber(vector<int>& nums) {
        vector<int> res(2, 0);
        int tmp = 0;
        for(auto num: nums)
            tmp = tmp ^ num;
        tmp = tmp & (~(tmp - 1));
        for(auto num: nums){
            if((num & tmp) == 0)
                res[0] ^= num;
            else res[1] ^= num;
        }
        return res;
    }
};

 

 

Guess you like

Origin blog.csdn.net/gjh13/article/details/90734039