231. Power of Two

2018-08-17  本文已影响0人  SilentDawn

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

猜你喜欢

热点阅读