0
点赞
收藏
分享

微信扫一扫

LeetCode知识点总结 - 231

代码小姐 2022-01-06 阅读 44

LeetCode 231. Power of Two

考点难度
RecursionEasy
题目

Given an integer n, return true if it is a power of two. Otherwise, return false.

An integer n is a power of two, if there exists an integer x such that n == 2^x.

思路

一个偶数和比它小1的奇数的二进制加起来等于0。

答案
public boolean isPowerOfTwo(int n) {
        return n > 0 && (n & n - 1) == 0;
}

参考:@linfq的答案

举报

相关推荐

0 条评论