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
相关推荐
muyun28003 小时前
History 模式 vs Hash 模式:Vue Router 技术决策因素详解
vue.js·算法·哈希算法
打马诗人4 小时前
【YOLO11】【DeepSort】【NCNN】使用YOLOv11和DeepSort进行行人目标跟踪。(基于ncnn框架,c++实现)
人工智能·算法·目标检测
瓦香钵钵鸡5 小时前
机器学习通关秘籍|Day 02:特征降维、用KNN算法和朴素贝叶斯实现分类
算法·机器学习·分类·贝叶斯·knn·超参数搜索·交叉验证
山烛7 小时前
决策树学习全解析:从理论到实战
人工智能·python·学习·算法·决策树·机器学习
修己xj8 小时前
探索设计模式的宝库:Java-Design-Patterns
算法
鲨鱼辣椒_TUT8 小时前
MySQL连接算法和小表驱动大表的原理
mysql·算法·adb
设计师小聂!8 小时前
力扣热题100------21.合并两个有序链表
算法·leetcode·链表
এ᭄画画的北北9 小时前
力扣-1.两数之和
数据结构·算法·leetcode
shenghaide_jiahu10 小时前
数学建模——递归和动态规划
算法·数学建模·动态规划