LeetCode 第 231 题 (Power of Two)
Given an integer, write a function to determine if it is a power of two.
这个题目有个特别简单的解法。当然,能够独自想出这个方法可不简单。这种方法可以作为一个知识点记住就好了。
class Solution {
public:
bool isPowerOfTwo(int n) {
if(n <= 0) return false;
return !(n & (n - 1));
}
};