Java no bit-wise really easy to understand 1 line solution 3 ms


  • 0
    A
    public boolean isPowerOfFour(int num) {
        return num>0 && Integer.highestOneBit(num) == num && Integer.numberOfTrailingZeros(num) % 2 == 0;
    }
    

    As long as we know if the number is power of 4 then it has 1 bit at highest position and following with even number of 0s, then it comes with this idea. Very straight forward and no need to convert it to string.


Log in to reply
 

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