当前位置:网站首页>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 .
边栏推荐
- 星瑞格数据库入围“2021年度福建省信息技术应用创新典型解决方案”
- Numpy -- epidemic data analysis case
- AE learning 01: AE complete project summary
- Three. JS introductory learning notes 0: illustration of how webgl and threejs work
- Three. JS introductory learning notes 11:three JS group composite object
- Function: JS Click to copy content function
- The "go to definition" in VS2010 does not respond or prompts the solution of "symbol not found"
- Talk about the cloud deployment of local projects created by SAP IRPA studio
- What about the pointer in neural network C language
- 企业级日志分析系统ELK
猜你喜欢

Shandong old age Expo, 2022 China smart elderly care exhibition, smart elderly care and aging technology exhibition

10 schemes to ensure interface data security

2022 the 4th China (Jinan) International Smart elderly care industry exhibition, Shandong old age Expo

Numpy --- basic learning notes

The unity vector rotates at a point

SPI master rx time out中断

2022第四届中国(济南)国际智慧养老产业展览会,山东老博会

Sysom case analysis: where is the missing memory| Dragon lizard Technology

Three. JS introductory learning notes 08:orbitcontrols JS plug-in - mouse control model rotation, zoom in, zoom out, translation, etc

C4D learning notes 2- animation - timeline and time function
随机推荐
Mysql database basic operation DQL basic query
iptables只允许指定ip地址访问指定端口
Numpy -- data cleaning
山东老博会,2022中国智慧养老展会,智能化养老、适老科技展
A link opens the applet code. After compilation, it is easy to understand
Three. JS introductory learning notes 19: how to import FBX static model
hellogolang
Shandong old age Expo, 2022 China smart elderly care exhibition, smart elderly care and aging technology exhibition
PyTorch 中的乘法:mul()、multiply()、matmul()、mm()、mv()、dot()
Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"
95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)
招标公告:福建省农村信用社联合社数据库审计系统采购项目(重新招标)
Migration and reprint
Three. JS introductory learning notes 0: illustration of how webgl and threejs work
Talk about the cloud deployment of local projects created by SAP IRPA studio
Postman generate timestamp, future timestamp
Syntaxhighlight highlights the right scroll bar
分步式监控平台zabbix
保证接口数据安全的10种方案
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