Java Easy Solution


  • 0

    Because the question says You could assume no leading zero bit in the integer’s binary representation., I just try to find the right number to do the xor operation.

        public int findComplement(int num) {
            int compare = 1;
            while (num > compare) {
                compare <<= 1;
                compare |= 1;
            }
            return num ^ compare;
        }
    

Log in to reply
 

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