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.
digits代表一个整数,将digits+1。
public class Solution {
public int[] plusOne(int[] digits) {
int carry=0;
int[] res=new int[digits.length+1];
res[0]=1;
int len=digits.length;
digits[len-1]+=1;
if(digits[len-1]>=10){
digits[len-1]-=10;
carry=1;
}
for(int i=len-2;i>=0;i--){
digits[i]=digits[i]+carry;
carry=0;
if(digits[i]>=10){
digits[i]-=10;
carry=1;
}
}
if(carry==1){
System.arraycopy(digits, 0, res, 1, digits.length);
return res;
}
return digits;
}
}