美文网首页
231. Power of Two

231. Power of Two

作者: YellowLayne | 来源:发表于2017-10-26 15:12 被阅读0次

1.描述

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

2.分析

若n&(n-1)的值为0,则n为2的整数次方。

3.代码

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

相关文章

网友评论

      本文标题:231. Power of Two

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