Leetcode 342. Power of Four

Problem

Given an integer n, return true if it is a power of four. Otherwise, return false.

An integer n is a power of four, if there exists an integer x such that n = = 4 x n == 4^x n==4x.

Algorithm

Use the bit operations.

Code

python3 复制代码
class Solution:
    def isPowerOfFour(self, n: int) -> bool:
        '''
        if n <= 0:
            return False
        m = int(log(n)/log(4))
        return 4 ** m == n
        '''
        if n <= 0:
            return False
        while n > 1:
            if n & 3 != 0:
                return False
            n >>= 2
        return True
相关推荐
做怪小疯子15 小时前
LeetCode 热题 100——子串——和为 K 的子数组
算法·leetcode·职场和发展
zl_vslam16 小时前
SLAM中的非线性优-3D图优化之李群李代数在Opencv-PNP中的应用(四)
人工智能·opencv·算法·计算机视觉
Run_Teenage19 小时前
C++:智能指针的使用及其原理
开发语言·c++·算法
mit6.82420 小时前
二维差分+前缀和
算法
民乐团扒谱机20 小时前
自然的算法:从生物进化到智能优化 —— 遗传算法的诗意与硬核“
算法
希望有朝一日能如愿以偿20 小时前
力扣每日一题:仅含1的子串数
算法·leetcode·职场和发展
漂流瓶jz21 小时前
SourceMap数据生成核心原理:简化字段与Base64VLQ编码
前端·javascript·算法
今天的砖很烫21 小时前
ThreadLocal 中弱引用(WeakReference)设计:为什么要 “故意” 让 Key 被回收?
jvm·算法
苏小瀚21 小时前
算法---FloodFill算法和记忆化搜索算法
数据结构·算法·leetcode
苏小瀚21 小时前
算法---二叉树的深搜和回溯
数据结构·算法