Naive clear self-explained solution


  • 0
    L

    I think this method should be the most self-explained one.

    public int findComplement(int num) {
            int res = 0, i = 0;
            
            while (num != 0){
                int bit = (num & 1) == 0 ? 1 : 0;
                res |= bit << (i++);
                num >>>= 1;
            }
            
            return res;
        }
    

Log in to reply
 

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