当前位置:网站首页>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 .
边栏推荐
- leetcode 241. Different ways to add parentheses design priority for operational expressions (medium)
- 95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)
- 过度依赖补助,大客户收款难,冲刺“国产数据库第一股”的达梦后劲有多足?
- Three. JS introductory learning notes 05: external model import -c4d into JSON file for web pages
- Logback日志框架第三方jar包 免费获取
- three.js打造酷炫下雪效果
- 【微信小程序】Chapter(5):微信小程序基础API接口
- Three. JS introductory learning notes 04: external model import - no material obj model
- Three. JS introductory learning notes 08:orbitcontrols JS plug-in - mouse control model rotation, zoom in, zoom out, translation, etc
- AB package details in unity (super detail, features, packaging, loading, manager)
猜你喜欢

Description of vs common shortcut keys

Three. JS introductory learning notes 10:three JS grid

Unity3D_ Class fishing project, bullet rebound effect is achieved

Numpy --- basic learning notes

AE learning 01: AE complete project summary

Xcode Revoke certificate

TiDB For PostgreSQL和YugabyteDB在Sysbench上的性能对比

分步式监控平台zabbix
通知Notification使用全解析

Logback日志框架第三方jar包 免费获取
随机推荐
航天宏图信息中标乌鲁木齐某单位数据库系统研发项目
torch. Numel action
Leetcode-231-2的幂
Postman generate timestamp, future timestamp
招标公告:盘锦市人民医院盘锦医院数据库维保项目
Three. JS introductory learning notes 19: how to import FBX static model
LeetCode2_ Add two numbers
There are many ways to realize the pause function in JS
Three. JS introductory learning notes 05: external model import -c4d into JSON file for web pages
MySQL中, 如何查询某一天, 某一月, 某一年的数据
Summary of knowledge points of xlua hot update solution
航运船公司人工智能AI产品成熟化标准化规模应用,全球港航人工智能/集装箱人工智能领军者CIMC中集飞瞳,打造国际航运智能化标杆
Leetcode-136-只出现一次的数(用异或来解答)
【微信小程序】Chapter(5):微信小程序基础API接口
Excessive dependence on subsidies, difficult collection of key customers, and how strong is the potential to reach the dream of "the first share of domestic databases"?
The "go to definition" in VS2010 does not respond or prompts the solution of "symbol not found"
Three. JS introductory learning notes 04: external model import - no material obj model
UE4 exports the picture + text combination diagram through ucanvasrendertarget2d
安科瑞电网智能化发展的必然趋势电力系统采用微机保护装置是
Particle effect for ugui