本题代码:https://github.com/doubleZ0108/Leetcode/blob/master/258.%E5%90%84%E4%BD%8D%E7%9B%B8%E5%8A%A0.py
- 解法1(T42% S57%): 基础做法,两重循环,外层不断循环直至唯一为止,里层不断循环%/逐位相加
- 解法2(T97% S76%): 要求 O ( 1 ) O(1) O(1)解完,观察到比如一个三位数num(abc) = 100a+10b+c,而我们要求的是a+b+c,将num稍微变形num = 99a+9b+(a+b+c),因此只要num对9取余,剩下的部分就是a+b+c。当然还要特别判断下如果一个大于10的数正好是9的倍数,最终应该返回的是9
class Solution(object):
def addDigits(self, num):
"""
:type num: int
:rtype: int
"""
# 解法2
if num < 10: return num
return num % 9 if num%9!=0 else 9
def otherSolution(self, num):
# 解法1
while num >= 10:
tmp = num
num = 0
while tmp:
num += tmp % 10
tmp //= 10
return num
本文介绍了两种Python解法来计算一个整数的各位数字之和。解法1采用两重循环,逐步求和;解法2利用数学技巧,通过数字对9取余快速得到结果。这种方法对于O(1)复杂度的需求非常有效。
582

被折叠的 条评论
为什么被折叠?



