LeetCode 1342 Number of Steps to Reduce a Number to Zero (Python)

Posted by 小明MaxMing on February 12, 2021

题目

Given a non-negative integer num, return the number of steps to reduce it to zero. If the current number is even, you have to divide it by 2, otherwise, you have to subtract 1 from it.

解题思路

按照题目要求模拟

代码

class Solution:
    def numberOfSteps (self, num: int) -> int:
        res = 0
        while num:
            num = num - 1 if num & 1 else num >> 1
            res += 1
        return res

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅