leetcode.258.Add Digits
Description
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.
Requirement
Could you do it without any loop/recursion in O(1) runtime?
Hint
题目描述中求出的值为输入的digital root。计算一个positive integer的digital root的公式为:
更多和digital root有关的知识见wiki:Digital Root
Result
class Solution(object):
def addDigits(self, num):
"""
:type num: int
:rtype: int
"""
return num - int((num - 1) / 9.0) * 9