当前位置:网站首页>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 .
边栏推荐
- Unity的三种单例模式(饿汉,懒汉,MonoBehaviour)
- Leetcode-231-2的幂
- 【花雕体验】15 尝试搭建Beetle ESP32 C3之Arduino开发环境
- 通知Notification使用全解析
- 企业级日志分析系统ELK
- Vite path alias @ configuration
- 强化实时数据管理,英方软件助力医保平台安全建设
- Eye of depth (VI) -- inverse of matrix (attachment: some ideas of logistic model)
- 招标公告:福建省农村信用社联合社数据库审计系统采购项目(重新招标)
- Three. JS introductory learning notes 04: external model import - no material obj model
猜你喜欢

2022第四届中国(济南)国际智慧养老产业展览会,山东老博会

After UE4 is packaged, mesh has no material problem

SPI master rx time out中断

分步式監控平臺zabbix

numpy---基础学习笔记

Vs tool word highlight with margin

Three. JS introductory learning notes 18: how to export JSON files with Blender

Shipping companies' AI products are mature, standardized and applied on a large scale. CIMC, the global leader in port and shipping AI / container AI, has built a benchmark for international shipping

How does geojson data merge the boundaries of regions?

Three. JS introductory learning notes 00: coordinate system, camera (temporarily understood)
随机推荐
尤雨溪,来了!
torch. Numel action
Shader_ Animation sequence frame
Description of vs common shortcut keys
Unity3d click events added to 3D objects in the scene
U3D_ Infinite Bessel curve
【花雕体验】15 尝试搭建Beetle ESP32 C3之Arduino开发环境
Bidding announcement: 2022 Yunnan Unicom gbase database maintenance public comparison and selection project (second) comparison and selection announcement
Please supervise the 2022 plan
航運船公司人工智能AI產品成熟化標准化規模應用,全球港航人工智能/集裝箱人工智能領軍者CIMC中集飛瞳,打造國際航運智能化標杆
航运船公司人工智能AI产品成熟化标准化规模应用,全球港航人工智能/集装箱人工智能领军者CIMC中集飞瞳,打造国际航运智能化标杆
Step by step monitoring platform ZABBIX
Three. JS introductory learning notes 00: coordinate system, camera (temporarily understood)
SysOM 案例解析:消失的内存都去哪了 !| 龙蜥技术
Notification uses full resolution
UE4 exports the picture + text combination diagram through ucanvasrendertarget2d
Three singleton modes of unity (hungry man, lazy man, monobehavior)
Leetcode-136-只出现一次的数(用异或来解答)
分步式监控平台zabbix
95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)