Plus One - Problem
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 zeros.
Increment the large integer by one and return the resulting array of digits.
Input & Output
Example 1 — Basic Case
$
Input:
digits = [1,2,3]
›
Output:
[1,2,4]
💡 Note:
The array represents 123, and 123 + 1 = 124. So we return [1,2,4].
Example 2 — Carry Required
$
Input:
digits = [4,3,2,1,9]
›
Output:
[4,3,2,2,0]
💡 Note:
The array represents 43219, and 43219 + 1 = 43220. The last digit 9 becomes 0 and we carry 1.
Example 3 — All Nines
$
Input:
digits = [9,9,9]
›
Output:
[1,0,0,0]
💡 Note:
The array represents 999, and 999 + 1 = 1000. We need an extra digit at the beginning.
Constraints
- 1 ≤ digits.length ≤ 100
- 0 ≤ digits[i] ≤ 9
- digits does not contain any leading zeros
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array [1,2,3] represents number 123
2
Process
Add 1 to the rightmost digit: 3 + 1 = 4
3
Output
Result [1,2,4] represents 124
Key Takeaway
🎯 Key Insight: Only digits equal to 9 create carries - most additions are simple!
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code