Java one line solution without for loop or recursions


  • 0
    B

    Here I just use the math to calculate the log value having base = 4.

        public boolean isPowerOfFour(int num) {
            return (Math.log(num)/Math.log(4) % 1== 0);
        }
    

    But I prefer the second one which is more readable:

    public boolean isPowerOfFour(int num) {
            double root = Math.log(num)/Math.log(4);
            return (root % 1 == 0);
        }
    

Log in to reply
 

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