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.
第一步:当num<10的时候,直接返回num。
第二步:当num>10的时候,把每一位加一遍,得出一个数字s。
第三步:把s代入第一步,第二步再去算
public int addDigits(int num) {
if (num < 10)
return num;
int s = 0;
while (num > 0) {
s += num % 10;
num /= 10;
}
if (s < 10)
return s;
else
return addDigits(s);
}