当前位置:网站首页>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 .
边栏推荐
- Apache Doris just "graduated": why should we pay attention to this kind of SQL data warehouse?
- Syntaxhighlight highlights the right scroll bar
- leetcode 241. Different ways to add parentheses design priority for operational expressions (medium)
- The "go to definition" in VS2010 does not respond or prompts the solution of "symbol not found"
- Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"
- What about the pointer in neural network C language
- 用手机在通达信上开户靠谱吗?这样炒股有没有什么安全隐患
- Three singleton modes of unity (hungry man, lazy man, monobehavior)
- The unity vector rotates at a point
- Aerospace Hongtu information won the bid for the database system research and development project of a unit in Urumqi
猜你喜欢
Unity3D_ Class fishing project, control the distance between collision walls to adapt to different models
Three. JS introductory learning notes 00: coordinate system, camera (temporarily understood)
Step by step monitoring platform ZABBIX
Three. JS introductory learning notes 11:three JS group composite object
torch.numel作用
AE learning 01: AE complete project summary
LeetCode2_ Add two numbers
Asynchronous application of generator function
TS as a general cache method
Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"
随机推荐
TS typescript type declaration special declaration field number is handled when the key key
Detailed explanation of unity hot update knowledge points and introduction to common solution principles
hellogolang
Asynchronous application of generator function
MySQL中, 如何查询某一天, 某一月, 某一年的数据
three.js打造酷炫下雪效果
融云斩获 2022 中国信创数字化办公门户卓越产品奖!
20th anniversary of agile: a failed uprising
Step by step monitoring platform ZABBIX
Three. JS introductory learning notes 07: external model import -c4d to JSON file for web pages -fbx import
Good news! Kelan sundb database and Hongshu technology privacy data protection management software complete compatibility adaptation
A JS script can be directly put into the browser to perform operations
深度之眼(七)——矩阵的初等变换(附:数模一些模型的解释)
There are many ways to realize the pause function in JS
分步式监控平台zabbix
Sysom case analysis: where is the missing memory| Dragon lizard Technology
TiDB For PostgreSQL和YugabyteDB在Sysbench上的性能对比
Plate - forme de surveillance par étapes zabbix
C4D learning notes 1- animation - animation key frames
Three singleton modes of unity (hungry man, lazy man, monobehavior)