Plus One

时间:2015-04-13 08:12:09   收藏:0   阅读:195

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.

solution:进位问题~~常考面试题~~

public class Solution {
    public int[] plusOne(int[] digits) {
      int carry=1;
        for(int i=digits.length-1;i>=0;i--){
            int d=digits[i]+carry;
            digits[i]=d%10;
            carry=d/10;
            if(carry==0){
                return digits;
            }
        }
            int[] res=new int[digits.length+1];
            res[0]=1;
            return res;  
    }
}

 

原文:http://www.cnblogs.com/joannacode/p/4421237.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!