1ms 99% java bitwise solution


  • 14
    S

    Example n = 8, bin(8) = 1000, bin(8-1) = 0111
    1000 & 0111 is 0 and powers of 2 are always 10(2), 100(4), 1000(8), 10000(16) and so on

     public boolean isPowerOfTwo(int n) {
            if(n < 1) return false;
            return (0 == ((n - 1) & n));
        }

  • 0
    J
    This post is deleted!

Log in to reply
 

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