Bit manipulation solution


  • 0
    int findComplement(int num) {
        int tmp = (1L << 32) - 1;
        int x = num;
        while (x > 0) {
            tmp <<= 1;
            x >>= 1;
        }
        return ~tmp & ~num;
    }
    

Log in to reply
 

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