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