当前位置:网站首页>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 .
边栏推荐
- hellogolang
- Notification uses full resolution
- torch. Numel action
- What about the pointer in neural network C language
- 用手机在通达信上开户靠谱吗?这样炒股有没有什么安全隐患
- Three. JS introductory learning notes 11:three JS group composite object
- Unity的三种单例模式(饿汉,懒汉,MonoBehaviour)
- The inevitable trend of the intelligent development of ankerui power grid is that microcomputer protection devices are used in power systems
- 一个普通人除了去工厂上班赚钱,还能干什么工作?
- Good news! Kelan sundb database and Hongshu technology privacy data protection management software complete compatibility adaptation
猜你喜欢

分步式監控平臺zabbix

神经网络c语言中的指针是怎么回事

Numpy -- epidemic data analysis case

LeetCode2_ Add two numbers

Xcode Revoke certificate

星瑞格数据库入围“2021年度福建省信息技术应用创新典型解决方案”

You Yuxi, coming!

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

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

Good news! Kelan sundb database and Hongshu technology privacy data protection management software complete compatibility adaptation
随机推荐
SysOM 案例解析:消失的内存都去哪了 !| 龙蜥技术
MySQL中, 如何查询某一天, 某一月, 某一年的数据
TS typescript type declaration special declaration field number is handled when the key key
保证接口数据安全的10种方案
Is it reliable to open an account on Tongda letter with your mobile phone? Is there any potential safety hazard in such stock speculation
Unity drawing plug-in = = [support the update of the original atlas]
强化实时数据管理,英方软件助力医保平台安全建设
如何在shell中实现 backspace
航运船公司人工智能AI产品成熟化标准化规模应用,全球港航人工智能/集装箱人工智能领军者CIMC中集飞瞳,打造国际航运智能化标杆
Three. JS introductory learning notes 03: perspective projection camera
47_Opencv中的轮廓查找 cv::findContours()
LeetCode1_ Sum of two numbers
Bidding announcement: Panjin people's Hospital Panjin hospital database maintenance project
用手机在通达信上开户靠谱吗?这样炒股有没有什么安全隐患
PyTorch 中的乘法:mul()、multiply()、matmul()、mm()、mv()、dot()
Application example of infinite list [uigridview]
Mysql database backup script
分步式監控平臺zabbix
AE learning 02: timeline
markdown公式编辑教程