1342. Number of Steps to Reduce a Number to Zero
Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 86% Topics: Math, Bit Manipulation
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(logn)
# Space: O(1)
class Solution(object):
def numberOfSteps (self, num):
"""
:type num: int
:rtype: int
"""
result = 0
while num:
result += 2 if num%2 else 1
num //= 2
return max(result-1, 0)
Solution from kamyu104/LeetCode-Solutions · MIT