1 line in JAVA


  • 0
    S
    return (int)(num ^ (long) Math.pow(2, 32 - Integer.numberOfLeadingZeros(num)) - 1);
    

    get the length of the num:

    32 - Integer.numberOfLeadingZeros(num)
    

    Get the same length as the binary format of num:

    (long) Math.pow(2, 32 - Integer.numberOfLeadingZeros(num)) - 1
    

Log in to reply
 

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