Skip to content

Commit 3426066

Browse files
chore: add LeetCode daily solution
1 parent 409f7c4 commit 3426066

5 files changed

Lines changed: 69 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Plus One (Easy)
2+
3+
**Problem ID:** 66
4+
**Date:** 2026-01-01
5+
**Link:** https://leetcode.com/problems/plus-one/
6+
7+
## Approach
8+
9+
To solve the "Plus One" problem, we need to increment a large integer represented as an array of its digits. The main idea is to simulate the process of addition, starting from the least significant digit (the rightmost element of the array) and moving towards the most significant digit (the leftmost element).
10+
11+
### Approach:
12+
13+
1. **Initialization**: Start from the last digit of the array. This is where the increment will first take effect.
14+
15+
2. **Increment and Carry**:
16+
- Add one to the last digit.
17+
- If this results in a value less than 10, simply update the digit and return the array, as no further carry is needed.
18+
- If the result is 10, set the current digit to 0 (because we have a carry) and move to the next digit to the left.
19+
20+
3. **Handling Carry**: Continue this process for each digit:
21+
- If a digit becomes 0 after adding the carry, repeat the carry operation for the next digit.
22+
- If you reach the most significant digit and it also results in a carry (e.g., from 9 to 10), you will need to insert a new digit at the beginning of the array (i.e., prepend 1).
23+
24+
4. **Return Result**: Once all digits are processed, return the modified array.
25+
26+
### Data Structures:
27+
- The solution primarily uses an array to represent the digits of the number. No additional complex data structures are needed.
28+
29+
### Complexity:
30+
- **Time Complexity**: O(n), where n is the number of digits in the array. In the worst case, we may need to traverse the entire array if there are multiple carries.
31+
- **Space Complexity**: O(1) if we ignore the output array, as we are modifying the input array in place. If a new digit is added, it can be considered O(n) in terms of the output size.
32+
33+
This approach efficiently handles the addition while respecting the constraints of the problem, ensuring that the solution is both straightforward and optimal.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int[] plusOne(int[] digits) {
3+
int n = digits.length;
4+
for (int i = n - 1; i >= 0; i--) {
5+
if (digits[i] < 9) {
6+
digits[i]++;
7+
return digits;
8+
}
9+
digits[i] = 0;
10+
}
11+
int[] result = new int[n + 1];
12+
result[0] = 1;
13+
return result;
14+
}
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var 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+
digits.unshift(1);
10+
return digits;
11+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def plusOne(self, digits: List[int]) -> List[int]:
3+
n = len(digits)
4+
for i in range(n - 1, -1, -1):
5+
if digits[i] < 9:
6+
digits[i] += 1
7+
return digits
8+
digits[i] = 0
9+
return [1] + digits

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,3 +322,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
322322
- 2025-12-29 — [Pyramid Transition Matrix](https://leetcode.com/problems/pyramid-transition-matrix/) (Medium) → `Medium/2025-12-29-756-Pyramid-Transition-Matrix`
323323
- 2025-12-30 — [Magic Squares In Grid](https://leetcode.com/problems/magic-squares-in-grid/) (Medium) → `Medium/2025-12-30-840-Magic-Squares-In-Grid`
324324
- 2025-12-31 — [Last Day Where You Can Still Cross](https://leetcode.com/problems/last-day-where-you-can-still-cross/) (Hard) → `Hard/2025-12-31-1970-Last-Day-Where-You-Can-Still-Cross`
325+
- 2026-01-01 — [Plus One](https://leetcode.com/problems/plus-one/) (Easy) → `Easy/2026-01-01-66-Plus-One`

0 commit comments

Comments
 (0)