題目在此 1342. Number of Steps to Reduce a Number to Zero
給定一個數字,如果是雙數請 除以 2
,不然就請減掉 1
解題思維
Just do it
程式碼
1 2 3 4 5 6 7 8 9 10 11 12 13
| class Solution: def numberOfSteps(self, num: int) -> int: result = 0 while num: if num % 2 == 0: num /= 2 else: num -= 1 result += 1 return result
|
也許你也會想看看