【leetcode】231-power-of-two

problem

231-power-of-two

 solution1

class Solution {
public:
    bool isPowerOfTwo(int n) {
        if(n==0) return false;
        while(n%2==0)
        {
            n /= 2;
        }
        return n==1;
    }
};
View Code

solution2

class Solution {
public:
    bool isPowerOfTwo(int n) {
        if(n<=0) return false;
        return !(n&(n-1));
    }
};
View Code

 

 

 

参考

1. Leetcode_231-power-of-two;

posted on 2018-12-29 14:46  鹅要长大  阅读(140)  评论(0编辑  收藏  举报

导航