当前位置:网站首页>Leetcode-231-2的幂
Leetcode-231-2的幂
2022-07-07 13:53:00 【_春天_】
题目描述
给定一个整数,编写一个函数来判断它是否是 2 的幂次方。
解题思路
判断一个整数是否是2的幂次方,可以利用2的幂次方的特性,把数字变成二进制来看。
我们知道,如果一个数字n是2的幂次方,那么它的二进制表示必定首位为1,其余位为0。而n-1的二进制表示中,首位变成了0,其余位是1.
例如,下图代表n=16时的二进制表示:
…… | 2 4 2^{4} 24 | 2 3 2^{3} 23 | 2 2 2^{2} 22 | 2 1 2^{1} 21 | 2 0 2^{0} 20 |
---|---|---|---|---|---|
n=16 | 1 | 0 | 0 | 0 | 0 |
n=15 | 0 | 1 | 1 | 1 | 1 |
&运算的特点是,相同为1,不同为0
这样的话,n和n-1做和运算(&),每位都是0&1,结果必然是0
也就是说如果n是2的幂次方,则n&(n-1)=0
如果n不是2的幂次方,结果则大于0
因此,利用&运算来判断
代码
代码一:正向计算
先贴上我2min之内完成的代码(laji code
import math
def isPowerOfTwo(self, n):
p = 0
if n <= 0:
return False
while True:
num = math.pow(2, p)
if n < num:
return False
elif n == num:
return True
else:
p += 1
continue
代码二:反向计算
def isPowerOfTwo(self, n):
if n:
while n % 2 == 0 :
n /= 2
return n == 1
return False
代码一和代码二的效率一样(一样低)
代码三:别人家的代码
def isPowerOfTwo(self, n):
if n < 1:
return False
return not (n & (n-1))
简洁明了,高效舒服。
边栏推荐
- leetcode 241. Different ways to add parentheses design priority for operational expressions (medium)
- 分步式监控平台zabbix
- Three. JS introductory learning notes 15: threejs frame animation module
- Vertex shader to slice shader procedure, varying variable
- Shipping companies' AI products are mature, standardized and applied on a large scale. CIMC, the global leader in port and shipping AI / container AI, has built a benchmark for international shipping
- hellogolang
- TCP framework___ Unity
- Is it reliable to open an account on Tongda letter with your mobile phone? Is there any potential safety hazard in such stock speculation
- Three. JS introductory learning notes 00: coordinate system, camera (temporarily understood)
- 一个普通人除了去工厂上班赚钱,还能干什么工作?
猜你喜欢
Asynchronous application of generator function
Syntax of generator function (state machine)
Async and await
Unity3D_ Class fishing project, bullet rebound effect is achieved
LeetCode1_ Sum of two numbers
Step by step monitoring platform ZABBIX
After UE4 is packaged, mesh has no material problem
Write sequence frame animation with shader
强化实时数据管理,英方软件助力医保平台安全建设
Apache Doris刚“毕业”:为什么应关注这种SQL数据仓库?
随机推荐
Three. JS introductory learning notes 15: threejs frame animation module
有钱人买房就是不一样
VS2005 strange breakpoint is invalid or member variable value cannot be viewed
过度依赖补助,大客户收款难,冲刺“国产数据库第一股”的达梦后劲有多足?
Clang compile link ffmpeg FAQ
A wave of open source notebooks is coming
A link opens the applet code. After compilation, it is easy to understand
C4D learning notes 3- animation - animation rendering process case
Introduction to pyGame games
PyTorch 中的乘法:mul()、multiply()、matmul()、mm()、mv()、dot()
Bidding announcement: Panjin people's Hospital Panjin hospital database maintenance project
hellogolang
C4D learning notes 2- animation - timeline and time function
Migration and reprint
Streaming end, server end, player end
When opening the system window under UE4 shipping, the problem of crash is attached with the plug-in download address
Good news! Kelan sundb database and Hongshu technology privacy data protection management software complete compatibility adaptation
Please supervise the 2022 plan
Three. JS introductory learning notes 08:orbitcontrols JS plug-in - mouse control model rotation, zoom in, zoom out, translation, etc
Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"