Easy to understand conceptually


  • 1
    P
    public int findComplement(int num) {
            int i=0, ans=1;
            
            do{
             i++;
             ans*=2;
            }while((int)Math.pow(2,i) <= num && i<31);
            
            return (ans-1-num);
            
        }
    

  • 0
    J

    Dang, that's a pretty elegant solution.
    That code in python looks like this:

        def findComplement(num):
            """
            :type num: int
            :rtype: int
            """
            i=0 
            ans=1;
            
            while 2**i <= num:
                i +=1
                ans *= 2
            
            return (ans-1-num)
    
    

Log in to reply
 

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