Plus One
Part of Practice: Digits & Points
A non-negative integer is given as an array of its decimal digits, most significant first, with no leading zeros. Add one to the number and return the resulting digit array.
The reason this is an array problem and not a one-liner: the number can have up to 100 digits, far beyond what fits in a built-in integer type, so you must do the grade-school addition on the digits yourself.
Examples
- Input:
- digits = [1,2,3]
- Output:
- [1,2,4]
123 + 1 = 124. No carry needed.
- Input:
- digits = [8,9,9]
- Output:
- [9,0,0]
899 + 1 = 900. The carry ripples through both 9s and stops at the 8.
- Input:
- digits = [9,9]
- Output:
- [1,0,0]
99 + 1 = 100. Every digit was 9, so the number grows one digit longer.
Hints
One at a time — struggle first, peek second.
Your thinking space — verify on LeetCode, then mark it.