leetcode 342. 4的幂(python)

1. 题目描述

  给定一个整数 (32 位有符号整数),请编写一个函数来判断它是否是 4 的幂次方。

示例 1:

输入: 16
输出: true
示例 2:

输入: 5
输出: false

2. 思路

  参考:https://blog.csdn.net/weixin_40163242/article/details/97396427 。

3.代码

class Solution:
    def isPowerOfFour(self, num: int) -> bool:
        temp = num & (num - 1);
        # 主要是要看num的奇数位上是否有1
        if(num > 0 and temp == 0 and num & 0x55555555) != 0:
            return True
        else:
            return False

 

posted @ 2019-08-19 23:07  有梦放飞  Views(225)  Comments(0Edit  收藏  举报