Description
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.
Follow up:
Could you do it without any loop/recursion in O(1) runtime?
Solution
Iterative
class Solution {
public int addDigits(int num) {
while (num > 9) {
int digits = 0;
while (num > 0) {
digits += num % 10;
num /= 10;
}
num = digits;
}
return num;
}
}
Optimized: mod, time O(1), space O(1)
牛掰啊,利用数学规律。注意mod 9为零需要特殊处理。
10^k % 9 = 1
a * 10^k % 9 = a % 9
Then let’s use an example to help explain.
Say a number x = 23456
x = 2* 10000 + 3 * 1000 + 4 * 100 + 5 * 10 + 6
2 * 10000 % 9 = 2 % 9
3 * 1000 % 9 = 3 % 9
4 * 100 % 9 = 4 % 9
5 * 10 % 9 = 5 % 9
Then x % 9 = ( 2+ 3 + 4 + 5 + 6) % 9, note that x = 2* 10000 + 3 * 1000 + 4 * 100 + 5 * 10 + 6
So we have 23456 % 9 = (2 + 3 + 4 + 5 + 6) % 9
class Solution {
public int addDigits(int num) {
if (num <= 0) return 0;
if (num % 9 == 0) return 9;
return num % 9;
}
}