We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
原题链接:https://leetcode-cn.com/problems/power-of-two/
解题思路:
8=00001000
6=00000110
n=8
00001000
11111000
8 & -8 = 00001000
n=6
00000110
11111010
6 & -6 = 00000010
n=7
00000111
11111001
7 & -7 = 00000001
(n & -n) === n
/** * @param {number} n * @return {boolean} */ var isPowerOfTwo = function (n) { // 判断n大于0,且满足(n & -n) === n。 return n > 0 && (n & -n) === n; };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
原题链接:https://leetcode-cn.com/problems/power-of-two/
解题思路:
8=00001000
。6=00000110
。n=8
为例,8的二进制为00001000
,-8的二进制为11111000
,8 & -8 = 00001000
等于8。n=6
为例,6的二进制为00000110
,-6的二进制为11111010
,6 & -6 = 00000010
不等于6。n=7
为例,7的二进制为00000111
,-7的二进制为11111001
,7 & -7 = 00000001
不等于7。(n & -n) === n
即可。如需查看更详细的分析,请看官方题解。The text was updated successfully, but these errors were encountered: