当前位置:网站首页>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
边栏推荐
猜你喜欢

Dichotomy, discretization, etc

Smart construction site "hydropower energy consumption online monitoring system"

Gbase database helps the development of digital finance in the Bay Area

2017 USP Try-outs C. Coprimes

【实战技能】如何做好技术培训?

Codeforces round 712 (Div. 2) d. 3-coloring (construction)
![[article de jailhouse] jailhouse hypervisor](/img/f4/4809b236067d3007fa5835bbfe5f48.png)
[article de jailhouse] jailhouse hypervisor

CCPC Weihai 2021m eight hundred and ten thousand nine hundred and seventy-five

剑指 Offer 35.复杂链表的复制

Sword finger offer 35 Replication of complex linked list
随机推荐
Sword finger offer 09 Implementing queues with two stacks
【Jailhouse 文章】Jailhouse Hypervisor
软件测试 -- 0 序
Sword finger offer 05 Replace spaces
CF1634E Fair Share
Hang wait lock vs spin lock (where both are used)
In this indifferent world, light crying
AtCoder Grand Contest 013 E - Placing Squares
CF1634 F. Fibonacci Additions
Light a light with stm32
F - Two Exam(AtCoder Beginner Contest 238)
26、 File system API (device sharing between applications; directory and file API)
Acwing 4300. Two operations
Gbase database helps the development of digital finance in the Bay Area
YOLOv5-Shufflenetv2
Introduction et expérience de wazuh open source host Security Solution
【实战技能】如何做好技术培训?
Drawing dynamic 3D circle with pure C language
How many checks does kubedm series-01-preflight have
Codeforces Round #716 (Div. 2) D. Cut and Stick