当前位置:网站首页>Daily question 1342 Number of operations to change the number to 0
Daily question 1342 Number of operations to change the number to 0
2022-07-05 05:42:00 【A big pigeon】
topic : Given num, If the current number is even , You need to divide it by 2 ; otherwise , subtract 1 . Ask for general num become 0 Required times .
Explain : Directly according to the meaning of the topic , Bit operation can improve computational efficiency
class Solution:
def numberOfSteps(self, num: int) -> int:
step = 0
while num:
if num&1: #num It's amazing , Equivalent to num%2 ==1
num -= 1
else:
num = num>>1 # Equivalent to num // 2
step += 1
return step
边栏推荐
- On the characteristics of technology entrepreneurs from Dijkstra's Turing Award speech
- Acwing 4301. Truncated sequence
- Demonstration of using Solon auth authentication framework (simpler authentication framework)
- [practical skills] how to do a good job in technical training?
- Hang wait lock vs spin lock (where both are used)
- Drawing dynamic 3D circle with pure C language
- PC register
- R语言【数据集的导入导出】
- Summary of Haut OJ 2021 freshman week
- 【云原生】微服务之Feign自定义配置的记录
猜你喜欢
Implement a fixed capacity stack
R语言【数据集的导入导出】
Hang wait lock vs spin lock (where both are used)
Sword finger offer 05 Replace spaces
Dichotomy, discretization, etc
【实战技能】如何做好技术培训?
【Jailhouse 文章】Jailhouse Hypervisor
剑指 Offer 04. 二维数组中的查找
Pointnet++ learning
shared_ Repeated release heap object of PTR hidden danger
随机推荐
Summary of Haut OJ 2021 freshman week
“磐云杯”中职网络安全技能大赛A模块新题
利用HashMap实现简单缓存
R语言【数据集的导入导出】
Reader writer model
sync.Mutex源码解读
Smart construction site "hydropower energy consumption online monitoring system"
Software test -- 0 sequence
Pointnet++学习
剑指 Offer 06.从头到尾打印链表
Alu logic operation unit
每日一题-搜索二维矩阵ps二维数组的查找
kubeadm系列-01-preflight究竟有多少check
动漫评分数据分析与可视化 与 IT行业招聘数据分析与可视化
On-off and on-off of quality system construction
数仓项目的集群脚本
Solution to the palindrome string (Luogu p5041 haoi2009)
Convolution neural network -- convolution layer
Control unit
【Jailhouse 文章】Look Mum, no VM Exits