[LeetCode][JavaScript]Plus One

Plus One

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.

https://leetcode.com/problems/plus-one/

 

 


 

 

大数加法。

 1 /**
 2  * @param {number[]} digits
 3  * @return {number[]}
 4  */
 5 var plusOne = function(digits) {
 6     var carry = 1;
 7     for(var i = digits.length - 1; i >= 0; i--){
 8         var sum = digits[i] + carry;
 9         if(sum === 10){
10             digits[i] = 0;
11             carry = 1;
12         }else{
13             digits[i] = sum;
14             carry = 0;
15         }
16     }
17     if(carry === 1){
18         digits.unshift(1);
19     }
20     return digits;
21 };

 

 

 

 

 

posted @ 2015-06-07 15:55  `Liok  阅读(773)  评论(0编辑  收藏  举报