Given a non-negative number represented as an array of digits, plus one to the number.

The digits are stored such that the most significant digit is at the head of the list.

Example

Given [1,2,3] which represents 123, return [1,2,4].

Given [9,9,9] which represents 999, return [1,0,0,0].

Solution

We can use a simple loop to add the result, with a carry variable.

At the end of the loop, carry would only be one if every digit of the input is 9, such as 999. In this case, the result is 1000.

public class Solution {
    /**
     * @param digits a number represented as an array of digits
     * @return the result
     */
    public int[] plusOne(int[] digits) {
        int carry = 1;
        for(int i = digits.length - 1; i >= 0; i--) {
            int a = digits[i] + carry;
            carry = a == 10 ? 1 : 0;
            digits[i] = a % 10;
        }
        
        if(carry == 1) {
            int[] copy = new int[digits.length + 1];
            copy[0] = 1;
            digits = copy;
        }
        
        return digits;
    }
}