[leetcode] 231. Power of Two

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

class Solution {
public:
    bool isPowerOfTwo(int n) {
        if(n<=0) return false;
        while(n>1)
        {
            if(n%2!=0) return false;
            n/=2;
        }
        return true;
    }
};

负数和0都不是power of 2. 

posted @ 2016-03-29 17:55  白天黑夜每日c  阅读(106)  评论(0编辑  收藏  举报