【LeetCode-342】Power of Four

来源:互联网 发布:腾讯分析 没数据 编辑:程序博客网 时间:2024/04/29 22:51

这是上一篇文章的升级版本,这是一解题的思想

# -*- encoding = 'utf-8' -*-__author__ = 'MG'import math as mclass 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


0 0
原创粉丝点击