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.

class Solution {
public:
int sum = getSum(num);
while (sum >= 10) {
sum = getSum(sum);
}
return sum;
}
private:
int getSum(int num) {
int sum = 0;
while (num) {
sum += num % 10;
num /= 10;
}
return sum;
}
};

• 本文已收录于以下专栏：

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit...
• sbitswc
• 2015年08月25日 13:18
• 7710

Given a non-negative integer num, repeatedly add all its digits until the result has only one digi...
• dream_hai
• 2015年10月09日 14:18
• 124

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit....
• wrsophia_977
• 2016年07月15日 17:30
• 106

• u012340794
• 2017年03月04日 16:02
• 103

//#258 Add Digits //28ms unknown distribution class Solution { public: int addDigits(int num) ...
• jerseyma
• 2015年09月02日 08:13
• 190

public int addDigits(int num) { int result = num; Integer I_num = new Integer(num); ...
• xinyuehuixin
• 2016年04月27日 11:38
• 118

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit....
• huaxixidongbeishida
• 2016年04月24日 21:34
• 125

## 258 Add Digits - LeetCode

Given a non-negative integer num, repeatedly add all its digits until the result has only one digi...
• tung_hit
• 2015年08月22日 14:04
• 183

1. 题目描述Given a non-negative integer num, repeatedly add all its digits until the result has only one...
• hanhanhanhuanghe
• 2015年09月17日 23:18
• 855