Leetcode题解之 —— 各位相加

思路


暴力遍历解法

题解


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
* @param {number} num
* @return {number}
*/
var addDigits = function (num) {
while (String(num).length !== 1) {
let total = 0;

while (num) {
total += num % 10;
num = ~~(num / 10);
}

num = total;
}

return num;
};