logo头像
Snippet 博客主题

【Leetcode 258】Add Digits

难度: 简单(Easy)

题目

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.

For example:

Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution(object):

def addDigits(self, num):
"""
:type num: int
:rtype: int
"""
while num > 9:
t = num
tot = 0
while t != 0:
tot += t%10
t /= 10
num = tot
return num

评论系统未开启,无法评论!