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
相关推荐
青山木3 分钟前
Hot 100 --- 搜索插入位置
java·数据结构·算法·leetcode
alexwang21134 分钟前
HDU 4348 详细题解
c++·算法·题解·hdu·主席树·可持久化数据结构·可持久化线段树
可编程芯片开发1 小时前
基于分段变步长搜索RMDCFT算法的天基雷达空间机动目标检测方法MATLAB仿真,对比RMDCFT算法
算法
2501_946736162 小时前
在线考试平台如何选型?从功能、优势与应用场景角度详解
大数据·人工智能·算法
天辛大师2 小时前
天心大师:不确定中锚定自我,AI生活的哲学命题
人工智能·算法·决策树·机器学习·生活·启发式算法
皓月斯语2 小时前
B2132 素数对
c++·算法·题解
星轨初途3 小时前
LeetCode 热题 100——day2 字母异位词分组
c++·算法·leetcode
happyprince3 小时前
篇1:整体观 · bitsandbytes项目全貌解读
人工智能·算法
青梅橘子皮3 小时前
STL---map/set... “家族“详解(从使用到底层)(1)
数据结构·算法
让学习成为一种生活方式4 小时前
DNA 甲基化综述(模式、调控与功能)--Current Opinion in Plant Biology
算法