题目地址
https://leetcode.com/problems/power-of-two/
题目大意
给出一个数,判断是否为2的幂数
解题思路
2的幂数,非常经典的问题,很直接,2的幂数n
满足以下条件:
(n & (n - 1)) == 0
源码
class Solution {
public:
bool isPowerOfTwo(int n) {
return n > 0 && (n & (n - 1)) == 0;
}
};
https://leetcode.com/problems/power-of-two/
给出一个数,判断是否为2的幂数
2的幂数,非常经典的问题,很直接,2的幂数n
满足以下条件:
(n & (n - 1)) == 0
class Solution {
public:
bool isPowerOfTwo(int n) {
return n > 0 && (n & (n - 1)) == 0;
}
};