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
相关推荐
luckycoding几秒前
42. 接雨水
leetcode
We་ct2 分钟前
LeetCode 39. 组合总和:DFS回溯解法详解
前端·算法·leetcode·typescript·深度优先·个人开发·回溯
小杍随笔3 分钟前
【Rust中所有符号的作用及使用场景详解】
java·算法·rust
MicroTech20258 分钟前
微算法科技(NASDAQ: MLGO)探索量子机器学习算法在预测模型中的应用,利用量子核方法提升复杂模式识别能力
科技·算法·机器学习
absunique1 小时前
算法设计模式看编程思维的抽象能力的技术6
算法·设计模式
DeepModel2 小时前
【概率分布】Beta分布详解
算法·概率论
我命由我123452 小时前
React - 验证 Diffing 算法、key 的作用
javascript·算法·react.js·前端框架·html·html5·js
Eward-an6 小时前
LeetCode 1980 题通关指南|3种解法拆解“找唯一未出现二进制串”问题,附Python最优解实现
python·算法·leetcode
程序员酥皮蛋6 小时前
hot 100 第四十题 40.二叉树的层序遍历
数据结构·算法·leetcode
木斯佳7 小时前
HarmonyOS 6实战:从爆款vlog探究鸿蒙智能体提取关键帧算法
算法·华为·harmonyos