首页 > 代码库 > LeetCode--Plus One

LeetCode--Plus One

这个题目与java里的BigInteger实现有些类似,可以参考其源码。

题目:

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.

解决方案:

public class Solution {
    public int[] plusOne(int[] digits) {
        for(int i=digits.length-1;i>=0;i--){
            int temp=digits[i]+1;
            if(temp>=10){
                digits[i]=temp%10;
            }else{
                digits[i]=temp;
                break;
            }
        }
        if(digits[0]==0){
            int[] re=new int[digits.length+1];
            re[0]=1;
            for(int j=1;j<re.length;j++){
                re[j]=digits[j-1];
            }
            return re;
        }
        return digits;
    }
}



LeetCode--Plus One