C++ 8 lines bit manipulation


  • 0
        vector<int> singleNumber(vector<int>& nums) {
            int diff=0;
            for(auto x:nums) diff^=x;
            diff&=-diff;  //get the last bit
            vector<int>res{0,0};
            for(auto x:nums)
                if(x&diff) res[0]^=x;
                else res[1]^=x;
            return res;
        }
    

Log in to reply
 

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