题解 | 2的幂
2的幂
https://www.nowcoder.com/practice/4a04240fd2be4e1287aab4af067f6d8f
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param n int整型
# @return bool布尔型
#
class Solution:
def poweroftwo(self , n: int) -> bool:
# write code here
if n==0:#输入为0,不存在直接返回(测试用例中有)
return False
res = 0#余数
while n>1:#一直除2,直至除尽
n, res = divmod(n,2)#整除结果,余数
if res!=0:#如果余数不等于0,则说明不能被2整除,不存在
return False
return True#剩余为1,说明存在

