【Python3】【力扣题】231. 2 的幂

【力扣题】题目描述:

此题:n为整数(32位有符号整数),x为整数。

2 的幂次方都大于0。若幂为负数,则0<n<1,n不为整数。

因此,n为正整数,x为0和正整数。

若二进制表示,则n的二进制只有1位是1,其余均为0。

最大2的幂为。

【Python3】代码:

1、解题思路:递归。依次n/2,判断余数是否为0,最终n为1,则n是2的幂。

知识点:递归:在函数中调用函数本身(需有退出条件,否则无限循环)。

python 复制代码
class Solution:
    def isPowerOfTwo(self, n: int) -> bool:
        if n <= 0: return False
        if n == 1: return True
        if n % 2 == 1: return False
        return self.isPowerOfTwo(n / 2)

2、解题思路:循环。依次n/2,判断余数是否为0,最终n为1,则n是2的幂。

python 复制代码
class Solution:
    def isPowerOfTwo(self, n: int) -> bool:
        if n <= 0: return False
        while n != 1 and n % 2 == 0:
            n = n / 2
        return n == 1 

3、解题思路:二进制表示。n的二进制只有1位为1。

(3-1)判断二进制中是否只有1位为1,若是,则n是2的幂。

知识点:bin(...):转为二进制字符串,即"0bxxx"。

序列**.**count(...):统计指定元素在序列(字符串、列表等)中有多少个。

python 复制代码
class Solution:
    def isPowerOfTwo(self, n: int) -> bool:
        return n > 0 and bin(n).count("1") == 1

(3-2)二进制与运算。

二进制与运算:两个二进制,相对应的每一位进行与运算。

1 & 1 = 1,1 & 0 = 0,0 & 0 = 0

① n 和 (n-1) 进行二进制与运算,若结果为0,则n是2的幂。

例如:假设8位二进制,最高位为符号位。n=8,二进制00001000,n-1为00000111,00001000&00000111=00000000。

python 复制代码
class Solution:
    def isPowerOfTwo(self, n: int) -> bool:
        return n > 0 and (n & (n-1)) == 0

② n 和 -n 进行二进制与运算,若结果为n,则n是2的幂。

-n的二进制表示:n的补码(反码+1),即(有符号位)最高位符号位不变,其余位全部取反再加1(即最高位不变,最低位的1以后不变,最高位和最低位1之间的位取反)。

例如:假设8位二进制,最高位为符号位。n=8,二进制00001000,-n为01111000,00001000&01111000=00001000。

python 复制代码
class Solution:
    def isPowerOfTwo(self, n: int) -> bool:
        return n > 0 and (n & -n) == n

4、解题思路:范围是32位有符号整数,将二进制1依次左移一位,判断是否与n相等,若是,则n是2的幂。

知识点:range(31):从0到30的数组(不含31),即0、1、2...30。【最高位为符号位】

python 复制代码
class Solution:
    def isPowerOfTwo(self, n: int) -> bool:
        for i in range(31):
            if n == (1 << i):
                return True
        return False

5、解题思路:判断是否是最大2的幂的约数(即能把n整除),若是,则n是2的幂。

整数a除以整数b(b≠0),除得商为整数,没有余数,则a是b的倍数,b是a的约数。

知识点:2 ** 30:。

1 << 30:左移,即1左移30位,为。

python 复制代码
class Solution:
    def isPowerOfTwo(self, n: int) -> bool:
        return n > 0 and (2 ** 30) % n == 0
        # 或者
        return n > 0 and (1 << 30) % n == 0

6、解题思路:math模块log2()方法。判断结果是否是integer整数。

知识点:math**.**log2(...):返回以2为底的对数。结果是浮点数。

int(...):转为整数。

float**.**is_integer():判断浮点数是否是integer整数。

例如:math**.**log2(8) 结果为3.0。【3.0是integer整数,和3的值相等。而3.1不是integer整数】

python 复制代码
class Solution:
    def isPowerOfTwo(self, n: int) -> bool:
        import math
        return n > 0 and math.log2(n) == int(math.log2(n))
        # 或者
        return n > 0 and math.log2(n).is_integer()
相关推荐
默_笙4 天前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
qq_426003964 天前
启动playwright录制codegen生成自动化测试脚本
python·自动化
虎头金猫4 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
长沙三为智能科技4 天前
家政小程序开发从0到上线:五阶段交付流程与验收清单
python
伞伞悦读4 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
只睡四小时4 天前
Canvas 弹道联机实战:700 行 + 固定时间步长
python·websocket·html5·游戏开发·canvas
旖旎夜光4 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
奇思妙想聪明勤奋的小羊4 天前
DeepAgents第5章:子Agent 与上下文隔离—让 Agent学会委派
人工智能·python·学习·语言模型
lpfasd1234 天前
2026年第38周GitHub趋势周报
python·科技·github
IZero074 天前
Jev 与 Laya
python·语言模型