美文网首页
231. Power of Two

231. Power of Two

作者: 铭小狮子酱 | 来源:发表于2020-06-09 07:01 被阅读0次

思路

2的倍数的二进制特点是首位为1,则满足
(n \& (n-1)) == 0
注意最外层的括号不能省。

代码(cpp)

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

相关文章

网友评论

      本文标题:231. Power of Two

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