【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;

原文地址:https://www.cnblogs.com/happyamyhope/p/10195958.html