标签:
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