美文网首页
231. Power of Two

231. Power of Two

作者: SilentDawn | 来源:发表于2018-08-17 08:35 被阅读0次

Problem

Given an integer, write a function to determine if it is a power of two.

Example

Input: 1
Output: true 
Explanation: 20 = 1
Input: 16
Output: true
Explanation: 24 = 16
Input: 218
Output: false

Code

static int var = [](){
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    return 0;
}();
class Solution {
public:
    bool isPowerOfTwo(int n) {
        if(n==1)
            return true;
        long i=2;
        while(i<n){
            i=i*2;
        }
        if(i==n)
            return true;
        return false;
    }
};

Result

231. Power of Two.png

相关文章

网友评论

      本文标题:231. Power of Two

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