[LeetCode]258. Add Digits (Easy) Python-2023.04.26
Posted on 2023-04-26 21:22 Uriel 閱讀(41) 評論(0) 編輯 收藏 引用 所屬分類: 閑來無事重切Leet Code 、大水題將一個數字不斷加和各數位的數字,直到結果的數小于10,水題
1 #258
2 #Runtime: 23 ms (Beats 46.91%)
3 #Memory: 13.3 MB (Beat 88.20%)
4
5 class Solution(object):
6 def addDigits(self, num):
7 """
8 :type num: int
9 :rtype: int
10 """
11 while num >= 10:
12 t = num
13 num = 0
14 while t > 0:
15 num += t % 10
16 t = t // 10
17 return num
2 #Runtime: 23 ms (Beats 46.91%)
3 #Memory: 13.3 MB (Beat 88.20%)
4
5 class Solution(object):
6 def addDigits(self, num):
7 """
8 :type num: int
9 :rtype: int
10 """
11 while num >= 10:
12 t = num
13 num = 0
14 while t > 0:
15 num += t % 10
16 t = t // 10
17 return num