当前位置:网站首页>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 .
边栏推荐
- 招标公告:福建省农村信用社联合社数据库审计系统采购项目(重新招标)
- Shader_ Animation sequence frame
- js中复选框checkbox如何判定为被选中
- Introduction to pyGame games
- When opening the system window under UE4 shipping, the problem of crash is attached with the plug-in download address
- 保证接口数据安全的10种方案
- Bidding announcement: Fujian Rural Credit Union database audit system procurement project (re bidding)
- C4D learning notes 3- animation - animation rendering process case
- TiDB For PostgreSQL和YugabyteDB在Sysbench上的性能对比
- Step by step monitoring platform ZABBIX
猜你喜欢

Wireless sensor networks -- ZigBee and 6LoWPAN
通知Notification使用全解析

C4D learning notes 3- animation - animation rendering process case

Odoo集成Plausible埋码监控平台

持续创作,还得靠它!

What about the pointer in neural network C language

torch. Numel action

UE4 exports the picture + text combination diagram through ucanvasrendertarget2d

The unity vector rotates at a point

Ue4/ue5 multi thread development attachment plug-in download address
随机推荐
安科瑞电网智能化发展的必然趋势电力系统采用微机保护装置是
谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题
Three. JS introductory learning notes 19: how to import FBX static model
Numpy -- data cleaning
Aerospace Hongtu information won the bid for the database system research and development project of a unit in Urumqi
Application example of infinite list [uigridview]
A wave of open source notebooks is coming
Postman generate timestamp, future timestamp
讲师征集令 | Apache SeaTunnel(Incubating) Meetup 分享嘉宾火热招募中!
numpy--数据清洗
Mesh merging under ue4/ue5 runtime
TiDB For PostgreSQL和YugabyteDB在Sysbench上的性能对比
Shader_ Animation sequence frame
Odoo集成Plausible埋码监控平台
Three. Introduction to JS learning notes 17: mouse control of 3D model rotation of JSON file
Bidding announcement: Panjin people's Hospital Panjin hospital database maintenance project
分类模型评价标准(performance measure)
C4D learning notes 2- animation - timeline and time function
Numpy -- epidemic data analysis case
Leetcode-136-只出现一次的数(用异或来解答)