Share 3 methods of 1-line java solution


  • 0
    C

    //solution 1:
    public boolean isPowerOfTwo(int n) {
    final double epsilon = 1.e-10;
    return (Math.log(n) / Math.log(2) + epsilon) % 1 <= 2 * epsilon;
    }

    //solution 2:
    public boolean isPowerOfTwo(int n) {
    return n > 0 && 1073741824 % n == 0;
    }

    //solution 3:
    public boolean isPowerOfTwo(int n) {
    return n>0 && Integer.toBinaryString(n).matches("^10*$");
    }


Log in to reply
 

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