-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0066_plus_one.js
More file actions
34 lines (32 loc) · 837 Bytes
/
0066_plus_one.js
File metadata and controls
34 lines (32 loc) · 837 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
/**
* @link https://leetcode.com/problems/plus-one/
* @description Increment the large integer by one and return the resulting array of digits.
* @param {Object []} digits
* @returns {Object}
*/
export default (digits) => {
const numDigits = digits.length;
const ans = new Array(numDigits).fill(0);
let currIndex = numDigits - 1;
while (currIndex >= 0) {
const currentDigit = digits[currIndex];
// Case 1: Current digit is less than 9
if (currentDigit < 9) {
ans[currIndex] = currentDigit + 1; //
break;
} else {
// Case 2: Current digit == 9
ans[currIndex] = 0;
currIndex -= 1;
}
}
// Case 3: All digits are 9s
if (currIndex < 0) {
return [1, ...ans];
}
while (currIndex > 0) {
currIndex -= 1;
ans[currIndex] = digits[currIndex];
}
return ans;
};