231. Power of Two
Given an integer
n, returntrueif it is a power of two. Otherwise, returnfalse.An integer
nis a power of two, if there exists an integerxsuch thatn == 2x.Example 1:
Input: n = 1
Output: true
Explanation: 20 = 1Example 2:
Input: n = 16
Output: true
Explanation: 24 = 16Example 3:
Input: n = 3
Output: falseConstraints:
-231 <= n <= 231 - 1Follow up: Could you solve it without loops/recursion?
def isPowerOfTwo(self, n: int) -> bool:
return n > 0 and (n & (n - 1)) == 0