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