public int[] plusOne(int[] digits) {
if(digits == null  digits.length == 0)
return new int[]{};
int carry = 1; //加1可以看成是有进位1
for(int i = digits.length  1; i >= 0; i) {
int sum = carry + digits[i];
if(sum == 10) {
carry = 1;
digits[i] = 0;
} else {
digits[i] += 1;
return digits;
}
}
int[] result = new int[digits.length+1]; //比原有的digits多出一位
result[0] = 1;
return result;
}
Simple Java solution with comments



@WuQiFu You have already set carry = 1 before the for loop. So carry would remain 1 in the if(sum == 10) block without setting it to 1 again. You could try to run the code without this line.
