342. Power of four

Problem

Given an integer (signed 32 bits), write a function to check whether it is a power of 4.
Follow up: Could you solve it without loops/recursion?

Example

Input: 16
Output: true
Input: 5
Output: false

Code

static int var= [](){
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    return 0;
}();
class Solution {
public:
    bool isPowerOfFour(int num) {
        int flag = 1;
        int i=0;
        do{
            if(num==flag)
                return true;
            flag = flag << 2;
        }while(++i<16);
        return false;
    }
};

Result

342. Power of four_第1张图片
342. Power of four.png

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