-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplus-one.js
More file actions
34 lines (30 loc) · 952 Bytes
/
plus-one.js
File metadata and controls
34 lines (30 loc) · 952 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
// https://leetcode.com/problems/plus-one/
// Related Topics: Array
// Difficulty: Easy
/*
Initial thoughts:
Looping over the array from the end backwards we are going to add one to
the value, always taking care that the value does not become greater than 9.
If there is no remainder(carry) we break out of the loop, otherwise we carry
the remainder to the digit to the left.
At the end of the loop we check to make sure that there is no carry remaining,
if there is some remainder, we are going to add it to the head of the array.
Time complexity: O(n) where n === digits.length
Space complexity: O(1)
*/
/**
* @param {number[]} digits
* @return {number[]}
*/
const plusOne = digits => {
let carry = 1;
for (let i = digits.length - 1; i >= 0; i--) {
let temp = digits[i] + carry;
carry = (temp / 10) | 0;
temp = temp % 10;
digits[i] = temp;
if (!carry) break;
}
if (carry) digits.unshift(carry);
return digits;
};