当前位置:网站首页>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 .
边栏推荐
- 用手机在通达信上开户靠谱吗?这样炒股有没有什么安全隐患
- LeetCode2_ Add two numbers
- AE learning 02: timeline
- [flower carving experience] 15 try to build the Arduino development environment of beetle esp32 C3
- Rongyun won the 2022 China Xinchuang digital office portal excellence product award!
- Numpy -- data cleaning
- 航運船公司人工智能AI產品成熟化標准化規模應用,全球港航人工智能/集裝箱人工智能領軍者CIMC中集飛瞳,打造國際航運智能化標杆
- Three. JS introductory learning notes 07: external model import -c4d to JSON file for web pages -fbx import
- C Alibaba cloud OSS file upload, download and other operations (unity is available)
- JS array foreach source code parsing
猜你喜欢
Notification uses full resolution
谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题
航运船公司人工智能AI产品成熟化标准化规模应用,全球港航人工智能/集装箱人工智能领军者CIMC中集飞瞳,打造国际航运智能化标杆
Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"
分步式監控平臺zabbix
What about the pointer in neural network C language
Description of vs common shortcut keys
C4D learning notes 3- animation - animation rendering process case
Three. JS introductory learning notes 00: coordinate system, camera (temporarily understood)
星瑞格数据库入围“2021年度福建省信息技术应用创新典型解决方案”
随机推荐
【微信小程序】Chapter(5):微信小程序基础API接口
Whole process analysis of unity3d rendering pipeline
Asynchronous application of generator function
Vite path alias @ configuration
持续创作,还得靠它!
LeetCode2_ Add two numbers
You Yuxi, coming!
深度之眼(七)——矩阵的初等变换(附:数模一些模型的解释)
Learn good-looking custom scroll bars in 1 minute
AE learning 01: AE complete project summary
Step by step monitoring platform ZABBIX
Leetcode-136-只出现一次的数(用异或来解答)
MySQL数据库基本操作-DQL-基本查询
统计学习方法——感知机
Three. JS introductory learning notes 00: coordinate system, camera (temporarily understood)
LeetCode1_ Sum of two numbers
Three. JS introductory learning notes 18: how to export JSON files with Blender
three.js打造酷炫下雪效果
2022山东智慧养老展,适老穿戴设备展,养老展,山东老博会
A wave of open source notebooks is coming