您的位置:首页 > 其它

【LeetCode-342】Power of Four

2016-04-20 15:34 411 查看
这是上一篇文章的升级版本,这是一解题的思想

# -*- encoding = 'utf-8' -*-
__author__ = 'MG'
import math as m

class Solution(object):
# 最low的一种解法了
def isPowerOfFour1(self, num):
"""
:type num: int
:rtype: bool
"""
if num < 1:
return False

temp = m.log(num,4)
if temp == int(temp):
return True
else:
return False

# 整除的方法(不符合这道题要求,这道题不允许循环)
def isPowerOfFour2(self, num):
if num < 1:
return False

while num % 4 == 0:
num = num / 4

return num == 1

def isPowerOfFour3(self, num):
if num < 1:
return False

# 前面一个条件是2的幂的判定方法,(4的幂,1在奇数位上)
return num & num - 1 == 0 and num & 0x55555555 == num

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息