2ms simple java solution based on positioning of 1 bit


  • 0
    F
    public boolean isPowerOfFour(int num) {
            if(num<1)return false;
            int count=0;
            int place=0;
            while(num!=0){
                int add=num & 1;
                count = count + add;
                place+=1;
                if(count>1 || (add==1&&place%2==0))
                    return false;
                num=num>>>1;
            }
            return true;
        }
    }
    

  • 0
    H

    could you please explain what does the variable count do? doesn't count the number of 1s in the number?


Log in to reply
 

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