LeetCode 第 231 題 (Power of Two)
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));
}
};
LeetCode 第 231 題 (Power of Two)