题目
题目链接
思路
数学问题,找规律
原数 | 结果 |
---|
0 | 0 |
1 | 1 |
2 | 2 |
3 | 3 |
4 | 4 |
5 | 5 |
6 | 6 |
7 | 7 |
8 | 8 |
9 | 9 |
10 | 1 |
11 | 2 |
12 | 3 |
13 | 4 |
14 | 5 |
15 | 6 |
16 | 7 |
17 | 8 |
18 | 9 |
19 | 1 |
20 | 2 |
由上述列表,我们发现,最终结果是以9为循环排列,当该数字能被9整除时输出9,当无法整除时输出除以9的余数。因为0除以9余0所以需要在判断是否整除9时特判。
Python3代码
一行代码解决
def addDigits(self, num: int) -> int:
return 9 if (num%9 == 0 and num != 0) else num%9