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.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容