当前位置:网站首页>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))
简洁明了,高效舒服。
边栏推荐
- Three. Introduction to JS learning notes 17: mouse control of 3D model rotation of JSON file
- The "go to definition" in VS2010 does not respond or prompts the solution of "symbol not found"
- 招标公告:2022年云南联通gbase数据库维保公开比选项目(第二次)比选公告
- Three. JS introductory learning notes 15: threejs frame animation module
- Strengthen real-time data management, and the British software helps the security construction of the medical insurance platform
- Three. JS introductory learning notes 11:three JS group composite object
- TCP framework___ Unity
- A wave of open source notebooks is coming
- 保证接口数据安全的10种方案
- numpy---基础学习笔记
猜你喜欢
通知Notification使用全解析
numpy---基础学习笔记
持续创作,还得靠它!
UE4 exports the picture + text combination diagram through ucanvasrendertarget2d
讲师征集令 | Apache SeaTunnel(Incubating) Meetup 分享嘉宾火热招募中!
Unity3D_ Class fishing project, bullet rebound effect is achieved
Three. JS introductory learning notes 15: threejs frame animation module
Mesh merging under ue4/ue5 runtime
航运船公司人工智能AI产品成熟化标准化规模应用,全球港航人工智能/集装箱人工智能领军者CIMC中集飞瞳,打造国际航运智能化标杆
C4D learning notes 2- animation - timeline and time function
随机推荐
leetcode 241. Different Ways to Add Parentheses 为运算表达式设计优先级(中等)
OpenGL common functions
Bidding announcement: 2022 Yunnan Unicom gbase database maintenance public comparison and selection project (second) comparison and selection announcement
U3D_ Infinite Bessel curve
webgl_ Enter the three-dimensional world (1)
Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"
航运船公司人工智能AI产品成熟化标准化规模应用,全球港航人工智能/集装箱人工智能领军者CIMC中集飞瞳,打造国际航运智能化标杆
Detailed explanation of Cocos creator 2.4.0 rendering process
用手机在通达信上开户靠谱吗?这样炒股有没有什么安全隐患
Streaming end, server end, player end
Numpy -- epidemic data analysis case
持续创作,还得靠它!
Syntaxhighlight highlights the right scroll bar
Three. JS introductory learning notes 04: external model import - no material obj model
Please supervise the 2022 plan
Align individual elements to the right under flex layout
Simple understanding and application of TS generics
Apache Doris just "graduated": why should we pay attention to this kind of SQL data warehouse?
It's different for rich people to buy a house
How does geojson data merge the boundaries of regions?