-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0066-plus-one.js
More file actions
52 lines (46 loc) · 1.4 KB
/
Copy path0066-plus-one.js
File metadata and controls
52 lines (46 loc) · 1.4 KB
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
52
//Blog:https://www.allenliservice.online/leetcode-js-66-plus-one/
// <strong>Solution:</strong>
// 1. 先陣列中的數字取出,由字串轉成數值。
// 2. 將數值 + 1。
// 3. 再將數值 + 1轉成字串陣列。
// (ex.注意當input的陣列大於2 ^ 53時,要使用BigInt(value),
// 因為JavaScript原生的Number能夠表示的最大值為2 ^ 53。)
// <strong>Code 1:</strong>
var plusOne = function (digits) {
if (digits.length > 11) {
let digitsNum = BigInt(digits.join(""));
digitsNum++;
digitsString = String(digitsNum);
return digitsString.split("");
}
let digitsNum = Number(digits.join(""));
digitsNum++;
digitsString = String(digitsNum);
return digitsString.split("");
};
/* <strong>Example 1</strong>
<pre style='background-color:#ggg'>
digits = [1,2,3]
digits.length = 3 < 11 //使用Number()
Number(digits.join('')) = 123 //先轉字串合併在一起,再轉數值
Number.(digits)++ = 123 + 1 = 124
String(digitsNum) = '124'
return digitsString.split('') = [1,2,4]
</pre> */
// <strong>Code 2:</strong>
var plusOne = function (digits) {
for (let i = digits.length - 1; i >= 0; i--) {
if (digits[i] !== 9) {
digits[i]++;
return digits;
} else {
digits[i] = 0;
}
}
digits.unshift(1);
return digits;
};
// <strong>Code 3:</strong>
var plusOne = function (digits) {
return String(BigInt(digits.join("")) + BigInt(1)).split("");
};