class Solution {
public:
bool isPowerOfTwo(int n) {
if (n <= 0) return false; // Zero and negative numbers are false.
if (n == 1) return true; // One is 2^0, so that's true.
if ((n%2) == 1) return false; // Any odd number is not a power of two.
return isPowerOfTwo(n/2); // If it's even, try dividing it by two.
}
};
|