-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathPlusOne.js
More file actions
32 lines (29 loc) · 941 Bytes
/
PlusOne.js
File metadata and controls
32 lines (29 loc) · 941 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
// Source : https://oj.leetcode.com/problems/plus-one/
// Author : Dean Shi
// Date : 2015-06-16
/**********************************************************************************
*
* Given a non-negative number represented as an array of digits, plus one to the number.
*
* The digits are stored such that the most significant digit is at the head of the list.
*
**********************************************************************************/
/**
* @param {number[]} digits
* @return {number[]}
*/
var plusOne = function(digits) {
var i = digits.length - 1;
while (i >= 0) {
if (++digits[i] < 10) {
return digits;
}
digits[i--] = 0;
}
digits.unshift(1);
return digits;
};
// Test cases
console.log(plusOne([9]).toString() === '1,0'); // true
console.log(plusOne([1, 0]).toString() === '1,1'); // true
console.log(plusOne([1, 2, 3]).toString() === '1,2,4'); // true