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.
只有当是9时,加1才会进位,否则返回即可。如果一直加到了末尾,产生了溢出,
要新建 一个数组。
public int[] plusOne(int[] digits) {
int carry=1,len=digits.length;;
for(int i=len-1;i>=0&&carry>0;i--){
int tmp=digits[i]+carry;
digits[i]=tmp%10;
carry=tmp/10;
}
if(carry==0)
return digits;
int[] arr=new int[len+1];
arr[0]=1;
for(int j=1;j<=len;j++)
arr[j]=digits[j-1];
return arr;
}