[LeeCode]Power of Two

Given an integer, write a function to determine if it is a power of two.

My initial code:

 class Solution:
# @param {integer} n
# @return {boolean}
def isPowerOfTwo(self, n):
if n==0 :
return False
if n==1 or n==2:
return True
if n % 2 != 0:
return False
if n < 4 and n
return self.isPowerOfTwo(n/2)

After google the internet, the best solution is:

class Solution:
# @param {integer} n
# @return {boolean}
def isPowerOfTwo(self, n):
if n<= 0 or n&(n-1) != 0:
return False
return True
上一篇:java控制执行流程


下一篇:php之 有点复杂的 流程管理