public int[] plusOne(int[] digits) {
int n = digits.length;
for(int i=n1; i>=0; i) {
if(digits[i] < 9) {
digits[i]++;
return digits;
}
digits[i] = 0;
}
int[] newNumber = new int [n+1];
newNumber[0] = 1;
return newNumber;
}
My Simple Java Solution

@WineYe By default java will initialize the whole array as 0. If we come to this stage of the code, we can be sure that the number was 99999*** something, so we can safely just change the first digit to 1 and leave all the rest 0.



@westworld said in My Simple Java Solution:
ethod runtime only beats
Well, I found it beats 36 percents of Java submission

nice solution, here's one basically the same, logic is a little more like how you would add by hand.
public int[] PlusOne(int[] digits) { int carry = 1; int index = digits.Length  1; while (index >= 0 && carry > 0) { digits[index] = (digits[index] + carry) % 10; carry = digits[index] == 0 ? 1 : 0; index; } if (carry > 0) { digits = new int[digits.Length + 1]; digits[0] = 1; } return digits; }


Nice. I wasn't sure how to solve the overflow, so I didn't even try to:
public int[] plusOne(int[] digits) { String digitsString = Arrays.stream(digits).mapToObj(String::valueOf).collect(joining("")); BigInteger plusOne = new BigInteger(digitsString).add(BigInteger.ONE); return plusOne.toString().chars().map(Character::getNumericValue).toArray(); }