LeetCode知识点总结 - 231
作者:互联网
LeetCode 231. Power of Two
考点 | 难度 |
---|---|
Recursion | Easy |
题目
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的答案
标签:知识点,return,power,two,231,integer,LeetCode 来源: https://blog.csdn.net/m0_59773145/article/details/122339393