参考:
- https://leetcode-cn.com/problems/add-digits/solution/java-o1jie-fa-de-ge-ren-li-jie-by-liveforexperienc/
- https://leetcode-cn.com/problems/add-digits/solution/python-hua-tu-jiang-ming-bai-o1zuo-fa-de-yuan-li-b/
数学解法为:
class Solution {
public int addDigits(int num) {
return (num - 1) % 9 + 1;
}
}
```
class Solution:
def addDigits(self, num: int) -> int:
while len(str(num)) != 1:
num = sum([int(i) for i in str(num)])
return num
```
class Solution {
public int addDigits(int num) {
int sum =0;
while ((num + "").length() != 1) {
while (num != 0) {
sum += num % 10;
num /=10;
}
num = sum;
sum = 0;
}
return num;
}
}