当前位置:网站首页>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 .
边栏推荐
- js中复选框checkbox如何判定为被选中
- Three. JS introductory learning notes 08:orbitcontrols JS plug-in - mouse control model rotation, zoom in, zoom out, translation, etc
- three.js打造酷炫下雪效果
- Limit of total fields [1000] in index has been exceeded
- Asynchronous application of generator function
- Numpy -- epidemic data analysis case
- Continuous creation depends on it!
- [flower carving experience] 15 try to build the Arduino development environment of beetle esp32 C3
- 如何在shell中实现 backspace
- A wave of open source notebooks is coming
猜你喜欢

C4D learning notes 3- animation - animation rendering process case

LeetCode3_ Longest substring without duplicate characters

Lecturer solicitation order | Apache seatunnel (cultivating) meetup sharing guests are in hot Recruitment!

Leetcode-231-2的幂

SPI master RX time out interrupt

Apache Doris just "graduated": why should we pay attention to this kind of SQL data warehouse?

Enterprise log analysis system elk

Description of vs common shortcut keys

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

LeetCode1_ Sum of two numbers
随机推荐
Three. JS introductory learning notes 18: how to export JSON files with Blender
[flower carving experience] 15 try to build the Arduino development environment of beetle esp32 C3
Postman generate timestamp, future timestamp
Particle effect for ugui
Enterprise log analysis system elk
航运船公司人工智能AI产品成熟化标准化规模应用,全球港航人工智能/集装箱人工智能领军者CIMC中集飞瞳,打造国际航运智能化标杆
C4D learning notes 2- animation - timeline and time function
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"?
A JS script can be directly put into the browser to perform operations
Please supervise the 2022 plan
Unity3d click events added to 3D objects in the scene
2022山东智慧养老展,适老穿戴设备展,养老展,山东老博会
Shandong old age Expo, 2022 China smart elderly care exhibition, smart elderly care and aging technology exhibition
通知Notification使用全解析
应用程序和matlab的通信方式
Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"
Vite path alias @ configuration
Virtual memory, physical memory /ram what
深度之眼(七)——矩阵的初等变换(附:数模一些模型的解释)
Leetcode-231-2的幂