You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's.
Increment the large integer by one and return the resulting array of digits.
Example 1:
Example 2:
Example 3:
Constraints:
public int[] plusOne(int[] digits) {
for(int i = digits.length - 1; i >=0; i--) {
if (digits[i] < 9) {
digits[i] += 1;
return digits;
}
digits[i] = 0;
}
// that is all digits are 9, new array with size + 1 and set MSB to '1'
int[] newDigits = new int[digits.length + 1];
newDigits[0] = 1;
return newDigits;
}