您的位置:首页 > 其它

[LeeCode]Power of Two

2015-07-19 15:28 176 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: