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
相关推荐
yi0117 小时前
LeetCode 134 加油站:从 双循环暴力 演变到 贪心
linux·算法·leetcode
sogw-三叶草️7 小时前
LeetCode 989. 数组形式的整数加法
数据结构·算法
维克兜率天8 小时前
【维克】均值回归:跌多了会涨,涨多了会跌
开发语言·笔记·python·算法·均值算法·回归·量化
yi0118 小时前
DAY 14: LeetCode 394. 字符串解码|递归和栈到底怎么处理嵌套?
数据结构·笔记·python·算法·leetcode
P.D.Wei9 小时前
[Leetcode 1614] 括号的最大嵌套深度
算法·leetcode·职场和发展
红姐跨境书9 小时前
AI 的底层逻辑:从数据、算法到智能的本质
人工智能·算法
繁华的地方不一定留下你的脚印9 小时前
C++ 算法与 ranges:用 find_if、transform 写清数据处理意图
开发语言·c++·算法
青山是哪个青山9 小时前
LeetCode 64:最小路径和
算法
blxr_9 小时前
力扣hot100寻找重复数https://leetcode.cn/problems/find-the-duplicate-number/
算法·leetcode·职场和发展
政企项目老覃9 小时前
金融医疗数据脱敏与隐私计算:从字段打码到联邦建模的落地复盘
程序人生·算法·金融·数据分析