LeetCode 342 Power of Four (Python)

Posted by 小明MaxMing on August 4, 2020

题目

Given an integer (signed 32 bits), write a function to check whether it is a power of 4.

解题思路

一个数是4的幂一定是2的幂,区别是对3取余的结果不同

代码

class Solution:
    def isPowerOfFour(self, num: int) -> bool:
        return num > 0 and num & (num - 1) == 0 and num % 3 == 1

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅