342. Power of four

2018-09-26  本文已影响0人  SilentDawn

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
上一篇下一篇

猜你喜欢

热点阅读