Leetcode-258题:Add Digits

题目

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.

代码

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

你可能感兴趣的:(Leetcode-258题:Add Digits)