342. Power of Four

Given an integer (signed 32 bits), write a function to check whether it is a power of 4.

Example:
Given num = 16, return true. Given num = 5, return false.

Follow up: Could you solve it without loops/recursion?

lass Solution {
public:
    bool isPowerOfFour(int num) {
        int n = num;
        if(n<=0 || n==2 || n==3) return false;
        if(n==1 ||n==4) return true;
        while(n>4){
            if(n%4) return false;
            n = n/ 4;
            if(n <4) return false;
            if(n==4) return true;
        }
    }
};


你可能感兴趣的:(342. Power of Four)