C++ 20ms (iterators)


  • 0
    P
    public:
        vector<int> singleNumber(vector<int>& nums) {
            int a = 0;
            auto numOne = 0;
            for(auto x = nums.begin(); x < nums.end(); ++x)
            {
                a ^= *x;
            }
            auto b = (a ^ (a - 1)) & a;
            for(auto x = nums.begin(); x < nums.end(); ++x)
            {   
                if(b   & *x)
                numOne ^= *x;
            }
            return vector<int>{numOne, a ^ numOne};
        
        }
    };
    

Log in to reply
 

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