public class Solution {
public int[] plusOne(int[] digits) {
if(digits.length == 0){
return digits;
}
int arraySize = digits.length  1;
int carry = 1;
if((digits[0] == 9) && (digits[arraySize] == 9)){
int[] result = new int[digits.length + 1];
result[0] = 1;
return result;
}
for (int i = arraySize; i >= 0; i) {
if(digits[i] == 9){
digits[i]= 0;
}
else if(digits[i] != 9){
digits[i] = digits[i] + carry;
return digits;
}
}
return digits;
}
}
My acceped Java solution


The middle part is not right, it couldn't deal with situation like {9, 0, 9}:
if((digits[0] == 9) && (digits[arraySize] == 9)){ int[] result = new int[digits.length + 1]; result[0] = 1; return result; }
You just need to change a little bit:
boolean everydigit = true; for (int j = 0; j <= leng; j++) { if(digits[j]!=9) everydigit = false; } if (everydigit == true) { int[] result = new int[digits.length + 1]; result[0] = 1; return result; }
Hope this could give you some idea :D