342. Power of Four [easy] (Python)

(LeetCode)Power of Four --- 4的次方,python实现 阅读详情

题目链接

https://leetcode.com/problems/power-of-four/

题目原文

Given an integer (signed 32 bits), write a function to check whether it is a power of 4.

Example:
Given num = 16, return true. Given num = 5, return false.

Follow up: Could you solve it without loops/recursion?

题目翻译

给定一个整数(有符号32位),写个函数判断它是否是4的倍数。比如:输入16,返回true;输入5,返回false。
进一步:你能不用循环或递归解决吗?

思路方法

思路一

先不考虑进一步的要求,用循环的方法求解。

代码

class Solution(object):
    def isPowerOfFour(self, num):
        """
        :type num: int
        :rtype: bool
        """
        if num <= 0:
            return False
        while num%4 == 0:
            num /= 4
        return num == 1

思路二

先不考虑进一步的要求,用递归的方法求解。

代码

class Solution(object):
    def isPowerOfFour(self, num):
        """
        :type num: int
        :rtype: bool
        """
        if num <= 0:
            return False
        if num == 1:
            return True
        if num%4 == 0:
            return self.isPowerOfFour(num/4)
        else:
            return False

思路三

题目说了不能循环或递归,上面的解法能AC但不太符合题意。考虑到输入是“Integer”,是有范围的(<2147483648),所以算出能输入的所有4的幂次,保存到list或dict中,对每次输入判断是否在这些数中即可。

代码

class Solution(object):
    def isPowerOfFour(self, num):
        """
        :type num: int
        :rtype: bool
        """
        nums = [1, 4, 16, 64, 256, 1024, 4096, 16384, 65536, 262144, 1048576, 4194304, 16777216, 67108864, 268435456, 1073741824]
        return num in nums

思路四

4的幂次表现在二进制上,为首位为1,后面有偶数个0。所以先将输入的数转换成2进制,再判断2进制中0和1的个数即可。
注意:Python里将数转成二进制后的表示为“0bxxxx”,最前面有个0,在计算0的个数时要考虑到。

代码

class Solution(object):
    def isPowerOfFour(self, num):
        """
        :type num: int
        :rtype: bool
        """
        return num > 0 and bin(num).count('1') == 1 and bin(num).count('0') % 2 == 1

思路五

类似思路四,我们可以先判断输入数的二进制形式为1后面若干个0,再判断1是否在奇数位即可。

代码

class Solution(object):
    def isPowerOfFour(self, num):
        """
        :type num: int
        :rtype: bool
        """
        return num > 0 and num&(num-1) == 0 and (num & 0x55555555) != 0

PS: 新手刷LeetCode,新手写博客,写错了或者写的不清楚还请帮忙指出,谢谢!
转载请注明:http://blog.csdn.net/coder_orz/article/details/51505994

在Ultralytics YOLO中添加/更换模块详细指南 使用参数继承 my_module_args: &my_args [64, 3, 0.5] # 定义参数模板 backbone: - [-1, 1, MyModule, *my_args] # 引用参数。 阅读详情

相关推荐

《Ventoy 制作系统安装 U 盘的具体步骤》

然后您可以在电脑的磁盘管理中对 U 盘进行快速格式化,选择 FAT32 或 NTFS 文件系统。至此,Ventoy 系统安装 U 盘就制作完成了。您可以在需要安装系统的电脑上,从 U 盘启动,然后选择相应的系统镜像进行安装。安装完成后,将您需要的操作系统镜像文件(如 Windows、Linux 等的 ISO 格式镜像)复制到 U 盘的根目录。在安装程序中,选择您插入的 U 盘设备,确保选择正确,以免误操作。2. 确认选择的 U 盘盘符正确,防止误操作导致其他磁盘数据丢失。点击“安装”按钮,等待安装完成。

jiyiwangluokeji的博客 2688

342. Power of Four

Given an integer (signed 32 bits), write afunction to check whether it is a power of 4. Example: Given num = 16, return true. Given num = 5,return false. Follow up: Could you solve it withoutloops/

Love_Taylor的博客 336

【AI生成】市面上主流自组网厂家的技术及市场研究

经过对全球自组网产业技术特征与市场格局的系统分析,本文得出以下核心结论:技术演进呈现智能化与融合化双轨路径 当前自组网技术创新已突破传统组网架构局限,形成"协议优化-架构创新-场景适配"的递进式发展路径。国际厂商聚焦智能算法应用,如Silvus采用深度学习实现动态拓扑优化,传输时延较传统方案降低37%;国内企业则侧重异构网络融合,中国移动设计院提出的5G LAN架构使工业场景通信可靠性提升至99.99%。

qq_31310977的博客 1701

342. Power of Four(4的幂)

给定一个整数,写一个函数来判断它是否是 4 的幂次方。如果是,返回 true;否则,返回 false。整数 n 是 4 的幂次方需满足:存在整数 x 使得n4x。

apprentice_eye的博客 463

Parrot OS最新安装教程

Parrot OS和Kali Linux一样是一个基于 Debian 的免费开源 Linux 发行版,适合安全专家、开发人员、以及关心隐私的人。相较于Kali,Parrot OS属于轻量级,对于硬件的要求无需很高,目前版本已更新到6.3。通过按照本教程的方法,能够成功安装Parrot OS。

🦸💻冰草花生酱提醒您“请记得检查所有电子设备的最后登录时间哦⏰🔍” 4450

WebDriverAgent 菜鸟解析篇

WebDriverAgent 是一个用于模拟移动设备浏览器的 Python 库,可以用来在 PC 上模拟移动设备的浏览器操作,从而进行自动化测试。下面是一个菜鸟对 WebDriverAgent 源码的解析篇,希望能对初学者有所帮助。

DJ355的博客 1232

Power of Four问题及解法

问题描述: Given an integer (signed 32 bits), write a function to check whether it is a power of 4. 示例: Given num = 16, return true. Given num = 5, return false. 问题分析: 一个数若是4的幂,那么它一定是2的幂,而且转换成二进制时

刘天的博客 458

别再折腾端口了!Kali Nethunter Kex 图形化桌面连接失败的真正原因与一键修复(附Termux命令)

本文深入分析了Kali Nethunter Kex图形化桌面连接失败的真正原因,包括端口配置变更、依赖缺失和权限问题,并提供了一键修复方案和Termux命令。通过详细的诊断步骤和优化配置,帮助用户快速恢复Kex功能,提升移动安全测试效率。

weixin_42530732的博客 425

leetcode 342. Power of Four

题目: Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Given num = 16, return true. Given num = 5, return false. Follow up: Could you solve it wit...

af1312的博客 245

一文解读DeepSeek训练数据集

本文重点以DeepSeek为例,详细介绍预训练数据集相关内容。大语言模型LLM(如DeepSeek、ChatGPT、文心一言等)的“智慧”源于它们学习的海量数据集。简单来说,数据集就是模型的“知识库”,通常由互联网公开的文本、图片、视频等。这些数据需满足两个特点:规模大和多样性。规模大是指数据量可达千亿级词汇,让模型学习复杂规律;多样性则涵盖多领域(如科技、文学)、多语言、多形式(对话、新闻),帮助模型适应不同场景。例如,训练数据中既有百科知识,也有日常对话,模型才能既解答专业问题,又能闲聊。

u013891230的博客 7112

Linux ARM平台开发系列讲解(GMSL摄像头篇)1.2 MAX9296 GMSL链路配置

串行器 +解串器组成,软件需要做的就是写这三部分的驱动,让摄像头数据能到达。3. 配置解串器和串行器的GMSL链路。串行器和解串器上电成功之后,就可以配置。软件配置(注意软件只可以配置速率,2. 9296上电,使能芯片。的版本和速率,比如版本是配置。配置成功之后,就会出现串行器。的链路,其它同类型的。设备地址,我设置的是。...

DSMGUOGUO的博客 1万+

342. Power of Four【位运算】

2017/3/23 22:23:57 Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Given num = 16, return true. Given num = 5, return false. Follow up: Could you ...

weixin_30571465的博客 113

高通SDX35:SGMII接口PHY RTL8211FSI-CG驱动适配

高通SDX35 RTL8211F SGMII

不秃头的程序猿Gabriel的博客 796

342. Power of Four【easy】【python】

Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Given num = 16, return true. Given num = 5, return false. Follow up: Could you solve it without

bbjzcyg的博客 320

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

【LeetCode】342. Power of Four 解题报告(Python)

Power of Four[LeetCode]https://leetcode.com/problems/power-of-four/Total Accepted: 9305 Total Submissions: 28083 Difficulty: EasyQuestion Given an integer, write a function to determine if it is a po

负雪明烛 1989

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

Leetcode 342. power of four

Leetcode 342. power of four Leetcode 342. power of four 工具: 0101 0101 0101 0101 0101 0101 0101 0101 (32bits) 1431655765 作用: 4的零次方: 0001 4的1次方: 00100 4的2次方: 10000 也就是说,1431655765这个十进制数是power of 4的...

Vincent_xzh的博客 400

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

342. Power of Four(python+cpp)

题目: 解释: 判断一个数是不是4的幂,这里用到一个知识就是 1.判断一个数是不是2的幂用n&amp;(n-1)==0,一个数是4的幂的前提是它是2的幂 2.4的次幂-1之后是3的倍数: 3.4的次幂的二进制表示中只有一个1,剩下的全是0,而且唯一的1右边有偶数个0,所以(num&gt;&gt;1)与0x5555555555 &amp;之后为1 ,0x55555555=10101010101010...

小湉湉的博客 236

[leetcode-342]Power of Four(java)

原题链接:https://leetcode.com/problems/power-of-four/ 思路:首先判断是否是2的幂,如果是的话,再去判断是否为4的幂,判断2的幂要求整数的bit位中只有一个1,而这可以根据num & (num -1)来判断。 得到结果后,最佳的方案是能找到1后面有多少个0,然后这个个数对2取余,如果为0,表示可以整除。但是我没找到方法。public class Sol

中华胡杨的专栏 1447

leetcode 342. Power of Four 判断一个数是否为4的幂

Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Given num = 16, return true. Given num = 5, return false. Follow up: Could you solve it without

记忆力不好的博客 3435

【LeetCode】Power of Four(4的幂)

这道题是LeetCode里的第342道题。 题目描述: 给定一个整数 (32 位有符号整数),请编写一个函数来判断它是否是 4的幂次方。 示例 1: 输入: 16 输出: true 示例 2: 输入: 5 输出: false 进阶: 你能不使用循环或者递归来完成本题吗? 解题代码: class Solution { public boolean isPowe...

『诞生之时已至,以此修正万象』 322

Python [Leetcode 342]Power of Four

题目描述: Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Given num = 16, return true. Given num = 5, return false. 解题思路: 位操作。 首先判断是不是只有一位数字为1,其余...

dkk37351的博客 180

【LeetCode】342. Power of Four

题目 给定一个整数(带符号的32位),写一个函数来检查它是否为4的幂。 例1: 输入:16 输出:true 例2: 输入:5 输出:false 我写这篇博客倒不是因为这个题难,而是在讨论中看见最高票的两个代码,被惊到了,记录一下。代码如下 Java 1-line (cheating for the purpose of not using loops) public boolean i...

盐与黑胡椒的博客 393

Power of Four - 判断这个数字是不是被4整除

在Leetcode上看到一道题目: Given an integer (singed 32 bits), write a function to check whether it is a power of 4. Example 1: Input: 16 Output: true. Example 2: Input: 5 Output: false. 其实这道题,博主之前有做过类似的,就是判断是不...

CHNMSCS 350

342. Power of Four(数值型)

problem: Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example 1: Input: 16 Output: true Example 2: Input: 5 Output: false tip: 判断一个数是否是4的幂级数。 solutions: 1.最...

Hcaroline 236

LeetCode 第 342 题(Power of Four)

LeetCode 第 342 题 Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Given num = 16, return true. Given num = 5, return false. Follow up: Co

Ivan 的专栏 5683
上一篇: 326. Power of Three [easy] (Python)
下一篇: 83. Remove Duplicates from Sorted List [easy] (Python)
coder_orz
博客等级 码龄14年 102粉丝 90原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值