题目描述:
示例:
示例 1:
输入: 1
输出: true
解释: 20 = 1
示例 2:
输入: 16
输出: true
解释: 24 = 16
示例 3:
输入: 218
输出: false
解答代码:
class Solution:def isPowerOfTwo(self, n: int) -> bool:num = 1while num < n:num *= 2if num == n:return Trueelse:return False
解答思路:
暴力求解。
位运算思路:

class Solution:def isPowerOfTwo(self, n: int) -> bool:return n > 0 and n & (n - 1) == 0
