首页 > 其他 > 详细

[Leetcode]--Plus One

时间:2014-01-26 19:28:50      阅读:338      评论:0      收藏:0      [点我收藏+]

Given a number represented as an array of digits, plus one to the number.

 

bubuko.com,布布扣
public class Solution {
    public int[] plusOne(int[] digits) {
        int carry = 0;
        int length = digits.length;
        
        digits[length-1] = digits[length -1] + 1;
        for(int i = length -1 ; i>=0; i--){
            digits[i] += carry;
            carry = 0;
            if(digits[i] >= 10) {
                carry = digits[i] /10;
                digits[i] = digits[i]%10;
            }
        }
        
        if(carry != 0) {
         int[] result = new int[length+1];
         result[0] = carry;
         
         for(int j = 1; j< length+1; j++){
             result[j] = digits[j-1];
         }
         return result;
        } else {
            return digits;
        }
        
    }
}
bubuko.com,布布扣

[Leetcode]--Plus One

原文:http://www.cnblogs.com/RazerLu/p/3534235.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!