当前位置:网站首页>Power of leetcode-231-2
Power of leetcode-231-2
2022-07-07 16:11:00 【_ Spring_】
Catalog
Title Description
Given an integer , Write a function to determine if it is 2 Power square .
Their thinking
Determine whether an integer is 2 Power square , You can use 2 Properties of the power of , Turn numbers into binary .
We know , If a number n yes 2 Power square , Then its binary representation must first be 1, The rest are 0. and n-1 In the binary representation of , The first became 0, The rest are 1.
for example , The figure below represents n=16 Binary representation of time :
…… | 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 |
& The characteristic of operation is , Same as 1, Different for 0
In this case ,n and n-1 Do and calculate (&), Everyone is 0&1, The result must be 0
That is to say if n yes 2 Power square , be n&(n-1)=0
If n No 2 Power square , The result is greater than 0
therefore , utilize & To judge by operation
Code
Code 1 : Forward calculation
Stick me first 2min Code completed within (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
Code 2 : Reverse calculation
def isPowerOfTwo(self, n):
if n:
while n % 2 == 0 :
n /= 2
return n == 1
return False
Code one is as efficient as code two ( As low as )
Code three : Someone else's code
def isPowerOfTwo(self, n):
if n < 1:
return False
return not (n & (n-1))
Simple and clear , Efficient and comfortable .
边栏推荐
- 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
- 20th anniversary of agile: a failed uprising
- [wechat applet] Chapter (5): basic API interface of wechat applet
- leetcode 241. Different Ways to Add Parentheses 为运算表达式设计优先级(中等)
- Three. JS introductory learning notes 13: animation learning
- 讲师征集令 | Apache SeaTunnel(Incubating) Meetup 分享嘉宾火热招募中!
- 持续创作,还得靠它!
- Continuous creation depends on it!
- TS typescript type declaration special declaration field number is handled when the key key
- 喜讯!科蓝SUNDB数据库与鸿数科技隐私数据保护管理软件完成兼容性适配
猜你喜欢
尤雨溪,来了!
Continuous creation depends on it!
Three. JS introductory learning notes 10:three JS grid
Three. JS introductory learning notes 11:three JS group composite object
A wave of open source notebooks is coming
持续创作,还得靠它!
torch.numel作用
Three. JS introductory learning notes 05: external model import -c4d into JSON file for web pages
Dotween -- ease function
SPI master RX time out interrupt
随机推荐
Three. JS introductory learning notes 04: external model import - no material obj model
融云斩获 2022 中国信创数字化办公门户卓越产品奖!
Talk about the cloud deployment of local projects created by SAP IRPA studio
【微信小程序】Chapter(5):微信小程序基础API接口
Mysql database basic operation DQL basic query
A wave of open source notebooks is coming
Is it reliable to open an account on Tongda letter with your mobile phone? Is there any potential safety hazard in such stock speculation
When opening the system window under UE4 shipping, the problem of crash is attached with the plug-in download address
分类模型评价标准(performance measure)
TCP framework___ Unity
Three. JS introductory learning notes 07: external model import -c4d to JSON file for web pages -fbx import
The inevitable trend of the intelligent development of ankerui power grid is that microcomputer protection devices are used in power systems
torch.numel作用
Use moviepy Editor clips videos and intercepts video clips in batches
Three. JS introduction learning notes 12: the model moves along any trajectory line
A JS script can be directly put into the browser to perform operations
MySQL数据库基本操作-DQL-基本查询
UE4 exports the picture + text combination diagram through ucanvasrendertarget2d
Learn good-looking custom scroll bars in 1 minute
The "go to definition" in VS2010 does not respond or prompts the solution of "symbol not found"