Add Digits
Desicription
Given a non-negative integer num
, repeatedly add all its digits until the result has only one digit.
Example:
代码语言:javascript复制Input: 38
Output: 2
Explanation: The process is like: 3 8 = 11, 1 1 = 2.
Since 2 has only one digit, return it.
Solution
代码语言:javascript复制class Solution {
private:
int getLength(int num) {
return static_cast<int>(log10(num) 1);
}
public:
int addDigits(int num) {
if(getLength(num) <= 1) {
return num;
}
int sum = 0;
while(num) {
sum = num % 10;
num /= 10;
}
return addDigits(sum);
}
};