美文网首页
342. Power of four

342. Power of four

作者: SilentDawn | 来源:发表于2018-09-26 20:03 被阅读0次

    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

    相关文章

      网友评论

          本文标题:342. Power of four

          本文链接:https://www.haomeiwen.com/subject/sgvjoftx.html