美文网首页
231. Power of Two

231. Power of Two

作者: 刘小小gogo | 来源:发表于2018-08-23 23:17 被阅读0次
image.png

技巧性非常强
如果是2的幂次方 则有(x & (x-1) == 0)

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

相关文章

网友评论

      本文标题:231. Power of Two

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