4 ms C solution with some Digital Logic tricks. O(1) space.


  • 3
    W
    /*
     * Each bit in y2 y1 is such that:
     * y2 y1 ::= 00 -> 01 -> 11 -> 00
     */
    int singleNumber(int *nums, int numsSize)
    {
        int y2 = 0, y1 = 0, t2, t1;
    
        while (--numsSize >= 0) {
            t2 = nums[numsSize] & y1;
            t1 = nums[numsSize] & ((~y2 & ~y1) | y2);
            y2 ^= t2;
            y1 ^= t1;
        }
        return y1;
    }

Log in to reply
 

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