-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0258-add-digits.js
More file actions
51 lines (42 loc) · 1012 Bytes
/
Copy path0258-add-digits.js
File metadata and controls
51 lines (42 loc) · 1012 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//Blog: https://www.allenliservice.online/leetcode-js-258-add-digits/
// <strong>Solution:</strong>
// 1. 如果 num 介於 0 ~9 之間,回傳 num。
// 2. 當(num > 9)時宣告 sum = 0,且
// 3. 當(num 的整數不等於 0),
// 執行以下:
// (1.) sum = 0 + 個位數
// (2.) num = num / 10
//
// 將 num = sum,回傳 num。
// <strong>Code 1:</strongc>
var addDigits = function (num) {
if (num < 10 && num >= 0) return num;
while (num > 9) {
let sum = 0;
while (parseInt(num) !== 0) {
sum += parseInt(num % 10);
num /= 10;
}
num = sum;
}
return num;
};
/* <strong>Example 1</strong>
<pre style='background-color:#ggg'>
Input: num = 38
38 !== 0
--> 3 + 8 --> 11
11 !== 0
--> 1 + 1 --> 2
2 < 9, return 2
</pre> */
// <strong>Code 2:</strongc>
var addDigits = function (num) {
if (num === 0) return 0;
if (num % 9 === 0) return 9;
return num % 9;
};
// <strong>Code 3:</strongc>
var addDigits = function (num) {
return 1 + ((num - 1) % 9);
};