-
Notifications
You must be signed in to change notification settings - Fork 70
/
res.js
38 lines (34 loc) · 831 Bytes
/
res.js
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
/**
* Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.
*
* You may assume the integer do not contain any leading zero, except the number 0 itself.
*
* The digits are stored such that the most significant digit is at the head of the list.
*
* res.js
* @authors Joe Jiang ([email protected])
* @date 2017-02-18 19:41:09
* @version $Id$
*/
/**
* @param {number[]} digits
* @return {number[]}
*/
let plusOne = function(digits) {
let carrybit = 1,
res = [];
for (let i = digits.length - 1; i >= 0; i--) {
let currentval = digits[i] + carrybit;
if (currentval>9) {
carrybit=1;
currentval%=10;
} else {
carrybit=0;
}
res.unshift(currentval)
}
if (carrybit===1) {
res.unshift(1);
}
return res;
};