1. What I learned
’/=’ vs ‘//=’
’/’ is dividing the first operand by the second and the result always has type float. ‘//’ is a division, rounded to the next smallest whole number.
ex) 5/2 = 2.5
ex) 5//2 = 2
2. How I sloved
When the number is even, I divided the number in half. And when the number is odd, I took one out of it. Whenever that happened, I added one to the ‘count’.
3. Code
class Solution:
def numberOfSteps (self, num: int) -> int:
count = 0
while num > 0:
if num%2 == 0:
num //= 2
else:
num -= 1
count += 1
return count
4. Result
Runtime : 16 ms(99.31%), Memory usage : 12.7 MB(100.00%)
(Runtime can be different by a system even if it is a same code.)