个人解题思路:
class Solution {
public:
bool isPowerOfTwo(int n) {
if (n == 1)
return true;
else if (n == 0)
return false;
else if (n%2 == 1)
return false;
else
return isPowerOfTwo(n/2);
}
};
官方建议题解:
class Solution {
public:
bool isPowerOfTwo(int n) {
return n > 0 && (n & (n - 1)) == 0;
}
};
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/power-of-two/solution/2de-mi-by-leetcode-solution-rny3/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。