码迷,mamicode.com
首页 > 编程语言 > 详细

[LeetCode][JavaScript]Plus One

时间:2015-06-07 17:05:32      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:

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 };

 

 

 

 

 

[LeetCode][JavaScript]Plus One

标签:

原文地址:http://www.cnblogs.com/Liok3187/p/4558558.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!