[leetcode]342. Power of Four

problem

342. Power of Four

solution1:loop;

class Solution {
public:
    bool isPowerOfFour(int num) {
        if(num<1) return false;
        while(num%4==0)
        {
            num /= 4;
        }
        return num==1;
        
    }
};

 solution

class Solution {
public:
    bool isPowerOfFour(int num) {
        return ( num>0 && !(num&(num-1)) && ((num-1)%3==0) ); 
        //return ( num>0 && (num&(num-1))==0 && ((num-1)%3==0) );//ok.
        //return ( num>0 && (num&(num-1)==0) && ((num-1)%3==0) );//err. 
    }
};

ref:

1. Leetcode_342_Power of Four;

2. GrandYang;

end

原文地址:https://www.cnblogs.com/happyamyhope/p/10417688.html