Java solution without using loop/recursion/regex.

  • 0
    public class Solution {
        public boolean isPowerOfFour(int num) {
            if(num<0 || num==0) return false;
            if(num==1 || num==4) return true;
            String strNum=Integer.toBinaryString(num);
            if(strNum.length()<3 || (strNum.length()-1)%2!=0) return false;
            return strNum.equals("1");

    if we take a close look at all the numbers that are power of four, it is obivious that those numbers are in 100^n (n is a non-negative integer) pattern.

    So, after converting num to binary-string and replacing all the "00" with "", we can easily find out whether num is a power of four by determine if the left string equals "1".

Log in to reply

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