一、题目
二、示例
三、代码
代码1:
class Solution:
def poweroftwo(self , n: int) -> bool:
return n>0 and n & (n-1) == 0
代码2:
class Solution:
def poweroftwo(self , n: int) -> bool:
while n>1 and n % 2 == 0:
n /= 2
return n == 1
四、算法说明
代码1: 将n
与n-1
进行“与”运算。
代码2: 判断 n%2
是不是偶数即可,也就是一直除以2
,判断最后的余数。
胡萝卜
2022年9月1日
我不知道将去向何方,但我已在路上! |
---|
时光匆匆,虽未曾谋面,却相遇于斯,实在是莫大的缘分,感谢您的到访 ! |