Sharing my 16ms C++ solution


  • 1
    T
    class Solution {
    public:
        vector<int> singleNumber(vector<int>& nums) {
            vector<int> result(2);
            int x, y;
            int n = nums.size(), i;
            int xXORy = 0;
            for(i=0; i<n; i++)
                xXORy = xXORy^nums[i];
                
            int lowBit = 1;
            while((lowBit&xXORy) != lowBit)
            {
                lowBit = lowBit << 1;
            }
            
            x = 0;
            for(i=0; i<n; i++)
            {
                if(lowBit&nums[i])
                    x = x^nums[i];
            }
            
            y = xXORy ^ x;
            
            result[0] = x;
            result[1] = y;
            return result;
        }
    };

Log in to reply
 

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