我是靠谱客的博主 谨慎咖啡豆,这篇文章主要介绍Leetcode 231 Power of Two,现在分享给大家,希望可以做个参考。

Leetcode 231 Power of Two

#include <math.h>
using namespace std;

class Solution {
public:
    bool isPowerOfTwo(int n) {
        //如果n&(n-1)是0,则n是2的n次方
        if ((n &(n - 1) )== 0 && n > 0)//要把n&(n-1)再用括号括起来
            return true;
        else
            return false;
    }
};

class Solution {
public:
    bool isPowerOfTwo(int n) {
        return n > 0 && 1073741824 % n == 0;//1073741824是2的30次方 ,0x7fffffff是int的最大
        //值,所以在这个最大值范围之内,2的30次方就是最大的2的n次方
    }
};

最后

以上就是谨慎咖啡豆最近收集整理的关于Leetcode 231 Power of Two的全部内容,更多相关Leetcode内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(160)

评论列表共有 0 条评论

立即
投稿
返回
顶部