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
相关推荐
君义_noip2 小时前
信息学奥赛一本通 1661:有趣的数列 | 洛谷 P3200 [HNOI2009] 有趣的数列
c++·算法·组合数学·信息学奥赛·csp-s
程序员:钧念2 小时前
深度学习与强化学习的区别
人工智能·python·深度学习·算法·transformer·rag
英英_3 小时前
MATLAB数值计算基础教程
数据结构·算法·matlab
一起养小猫3 小时前
LeetCode100天Day14-轮转数组与买卖股票最佳时机
算法·leetcode·职场和发展
hele_two3 小时前
快速幂算法
c++·python·算法
l1t4 小时前
利用DeepSeek将python DLX求解数独程序格式化并改成3.x版本
开发语言·python·算法·数独
jllllyuz4 小时前
基于子集模拟的系统与静态可靠性分析及Matlab优化算法实现
算法·matlab·概率论
程序员-King.5 小时前
day143—递归—对称二叉树(LeetCode-101)
数据结构·算法·leetcode·二叉树·递归
BlockChain8885 小时前
字符串最后一个单词的长度
算法·go
爱吃泡芙的小白白5 小时前
深入解析:2024年AI大模型核心算法与应用全景
人工智能·算法·大模型算法