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.
in out
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
...
public int addDigits(int num) {
if(num == 0)return num;
return (num % 9 == 0) ? 9 : num % 9;
}