[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

posted on 2019-02-22 12:30  鹅要长大  阅读(184)  评论(0编辑  收藏  举报

导航