当前位置:网站首页>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 .
边栏推荐
- Application example of infinite list [uigridview]
- Three. JS introductory learning notes 11:three JS group composite object
- numpy--疫情数据分析案例
- leetcode 241. Different ways to add parentheses design priority for operational expressions (medium)
- Description of vs common shortcut keys
- AE learning 02: timeline
- TS typescript type declaration special declaration field number is handled when the key key
- Three. JS introductory learning notes 00: coordinate system, camera (temporarily understood)
- There are many ways to realize the pause function in JS
- [wechat applet] Chapter (5): basic API interface of wechat applet
猜你喜欢

Odoo集成Plausible埋码监控平台

AE learning 02: timeline

Three. JS introductory learning notes 03: perspective projection camera

喜讯!科蓝SUNDB数据库与鸿数科技隐私数据保护管理软件完成兼容性适配

统计学习方法——感知机

95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)

Step by step monitoring platform ZABBIX

Talk about the cloud deployment of local projects created by SAP IRPA studio

Postman generate timestamp, future timestamp

过度依赖补助,大客户收款难,冲刺“国产数据库第一股”的达梦后劲有多足?
随机推荐
Eye of depth (VII) -- Elementary Transformation of matrix (attachment: explanation of some mathematical models)
Vite path alias @ configuration
[excelexport], Excel to Lua, JSON, XML development tool
Bidding announcement: 2022 Yunnan Unicom gbase database maintenance public comparison and selection project (second) comparison and selection announcement
Numpy --- basic learning notes
Shader_ Animation sequence frame
Vs tool word highlight with margin
C4D learning notes 2- animation - timeline and time function
Strengthen real-time data management, and the British software helps the security construction of the medical insurance platform
Summary of knowledge points of xlua hot update solution
保证接口数据安全的10种方案
A link opens the applet code. After compilation, it is easy to understand
Odoo集成Plausible埋码监控平台
Three. JS introductory learning notes 05: external model import -c4d into JSON file for web pages
Talk about the cloud deployment of local projects created by SAP IRPA studio
Use moviepy Editor clips videos and intercepts video clips in batches
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
Mesh merging under ue4/ue5 runtime
hellogolang
Enterprise log analysis system elk