Home
DSA Patterns
Two Pointers
Plus One
Plus One (LeetCode 66)
Plus One (LeetCode 66): add one to a number stored as an array of digits: walk from the end, turn trailing 9s into 0s, and grow the array only when every digit was 9.
Problem Understanding
Plus One: a non-negative integer is given as an array digits, most significant digit first, with no leading zeros. Return the array for that integer plus one.
Examples: [1,2,3] returns [1,2,4], [4,3,2,1] returns [4,3,2,2], and [9] returns [1,0].
The Intuition: The Carry Stops at the First Digit Below 9
Adding 1 by hand only changes digits from the right until a digit can absorb it:
- A digit below 9 takes the
+1and nothing else changes, so you can return immediately. - A 9 becomes 0 and the 1 carries one place left.
- If the carry runs off the front, every digit was 9; the answer is a 1 followed by that many zeros.
It is Add Strings with the second number fixed at 1, so the loop can stop early instead of always walking the whole array. The same right-to-left carry drives Add Binary and Multiply Strings.
Plus One Visualization
TINTED = A 9 THAT ROLLED OVER TO 0 · GREEN = WHERE THE +1 LANDED
START AT THE LAST DIGIT
0
1
i
2
carry
1
length
3
Add 1 to the last digit, index 2
digits
i marks where the carry is. 9s that rolled over to 0 are tinted, and the digit that took the +1 turns green. Try All nines to see the array grow.
Plus One Dry Run
Input: digits = [8, 9, 9, 9]
| i | digits[i] | Action | digits after |
|---|---|---|---|
| 3 | 9 | set 0, carry | [8, 9, 9, 0] |
| 2 | 9 | set 0, carry | [8, 9, 0, 0] |
| 1 | 9 | set 0, carry | [8, 0, 0, 0] |
| 0 | 8 | 8 + 1 = 9, return | [9, 0, 0, 0] |
Answer: [9, 0, 0, 0].
Plus One LeetCode Solution
One loop from the right that returns as soon as a digit absorbs the carry. C always returns a newly allocated array, because LeetCode frees the result.
Plus One Code
1var plusOne = function(digits) {2 for (let i = digits.length - 1; i >= 0; i--) {3 if (digits[i] < 9) {4 digits[i]++;5 return digits;6 }7 digits[i] = 0;8 }9 return [1, ...digits];10};
Plus One in Python, Java, C++, TypeScript and C
Plus One in Python
class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
for i in range(len(digits) - 1, -1, -1):
if digits[i] < 9:
digits[i] += 1
return digits
digits[i] = 0
return [1] + digitsPlus One in Java
class Solution {
public int[] plusOne(int[] digits) {
for (int i = digits.length - 1; i >= 0; i--) {
if (digits[i] < 9) {
digits[i]++;
return digits;
}
digits[i] = 0;
}
int[] result = new int[digits.length + 1];
result[0] = 1;
return result;
}
}Plus One in C++
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
for (int i = digits.size() - 1; i >= 0; i--) {
if (digits[i] < 9) {
digits[i]++;
return digits;
}
digits[i] = 0;
}
digits.insert(digits.begin(), 1);
return digits;
}
};Plus One in TypeScript
function plusOne(digits: number[]): number[] {
for (let i = digits.length - 1; i >= 0; i--) {
if (digits[i] < 9) {
digits[i]++;
return digits;
}
digits[i] = 0;
}
return [1, ...digits];
}Plus One in C
int* plusOne(int* digits, int digitsSize, int* returnSize) {
int* result = calloc(digitsSize + 1, sizeof(int));
for (int i = digitsSize - 1; i >= 0; i--) {
if (digits[i] < 9) {
digits[i]++;
memcpy(result, digits, digitsSize * sizeof(int));
*returnSize = digitsSize;
return result;
}
digits[i] = 0;
}
result[0] = 1;
*returnSize = digitsSize + 1;
return result;
}Edge Cases & Common Mistakes
- All 9s:
[9, 9]becomes[1, 0, 0]; remember to grow the array. - Single digit:
[0]becomes[1], and[9]becomes[1, 0]. - Continuing after the +1: once a digit below 9 is incremented the answer is final; keep looping and you add 1 again.
- Converting to an integer: up to 100 digits overflow 64-bit types.
- Building the grown array: only the leading 1 is set; every other digit is already 0.
Summary
Walk from the last digit. A digit below 9 takes the +1 and you return; a 9 becomes 0 and the carry moves left. If every digit was 9, return a 1 followed by zeros. O(n) time, O(1) extra space except when the array grows.
More practice: DSA interview questions with answers · patterns cheat sheet
