258. Add Digits
[思路]
数字累加,将给定的一个整数,将个位,十位,百位等相加,连续操作,直到最后的值为个位数;
这是数学问题:树根
For base b (decimal case b = 10), the digit root of an integer is:
dr(n) = 0 if n == 0
dr(n) = (b-1) if n != 0 and n % (b-1) == 0
dr(n) = n mod (b-1) if n % (b-1) != 0
or
dr(n) = 1 + (n - 1) % 9
Note here, when n = 0, since (n - 1) % 9 = -1, the return value is zero (correct).
int addDigits(int num) {
return 1 + (num - 1) % 9;
}
网友评论