Skip to content

Commit fd93ef0

Browse files
Added solution for plus-one
1 parent 5e2d714 commit fd93ef0

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
---
2+
id: plus-one
3+
title: Plus One (LeetCode)
4+
difficulty: Easy
5+
sidebar_label: 0066-PlusOne
6+
topics:
7+
- Array
8+
---
9+
10+
## Problem Description
11+
12+
| Problem Statement | Solution Link | LeetCode Profile |
13+
| :---------------- | :------------ | :--------------- |
14+
| [Merge Two Sorted Lists](https://leetcode.com/problems/plus-one/) | [Merge Two Sorted Lists Solution on LeetCode](https://leetcode.com/problems/plus-one/solutions/) | [VijayShankerSharma](https://leetcode.com/u/darkknight648/) |
15+
16+
## Problem Description
17+
18+
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 0's.
19+
20+
Increment the large integer by one and return the resulting array of digits.
21+
22+
### Examples
23+
24+
#### Example 1
25+
26+
- **Input:** digits = [1,2,3]
27+
- **Output:** [1,2,4]
28+
- **Explanation:** The array represents the integer 123. Incrementing by one gives 123 + 1 = 124. Thus, the result should be [1,2,4].
29+
30+
#### Example 2
31+
32+
- **Input:** digits = [4,3,2,1]
33+
- **Output:** [4,3,2,2]
34+
- **Explanation:** The array represents the integer 4321. Incrementing by one gives 4321 + 1 = 4322. Thus, the result should be [4,3,2,2].
35+
36+
#### Example 3
37+
38+
- **Input:** digits = [9]
39+
- **Output:** [1,0]
40+
- **Explanation:** The array represents the integer 9. Incrementing by one gives 9 + 1 = 10. Thus, the result should be [1,0].
41+
42+
### Constraints
43+
44+
- `1 <= digits.length <= 100`
45+
- `0 <= digits[i] <= 9`
46+
- `digits` does not contain any leading 0's.
47+
48+
### Approach
49+
50+
To increment the large integer represented by the `digits` array, we can follow these steps:
51+
52+
1. Start from the least significant digit.
53+
2. Increment the last digit by 1.
54+
3. If the last digit becomes 10 after incrementing, set it to 0 and carry over 1 to the next digit.
55+
4. Repeat this process until there's no carry left or we reach the most significant digit.
56+
5. If there's a carry left after processing all digits, insert it at the beginning of the `digits` array.
57+
58+
### Solution Code
59+
60+
#### Python
61+
62+
```
63+
class Solution(object):
64+
def plusOne(self, digits):
65+
for i in range(len(digits) - 1, -1, -1):
66+
if digits[i] < 9:
67+
digits[i] += 1
68+
return digits
69+
digits[i] = 0
70+
return [1] + digits
71+
```
72+
73+
#### C++
74+
75+
```
76+
class Solution {
77+
public:
78+
vector<int> plusOne(vector<int>& digits) {
79+
int n = digits.size();
80+
int carry = 1;
81+
for (int i = n - 1; i >= 0; --i) {
82+
digits[i] += carry;
83+
carry = digits[i] / 10;
84+
digits[i] %= 10;
85+
}
86+
if (carry) {
87+
digits.insert(digits.begin(), carry);
88+
}
89+
return digits;
90+
}
91+
};
92+
```
93+
94+
#### Java
95+
96+
```
97+
class Solution {
98+
public int[] plusOne(int[] digits) {
99+
int n = digits.length;
100+
int carry = 1;
101+
for (int i = n - 1; i >= 0; --i) {
102+
digits[i] += carry;
103+
carry = digits[i] / 10;
104+
digits[i] %= 10;
105+
}
106+
if (carry != 0) {
107+
int[] result = new int[n + 1];
108+
result[0] = carry;
109+
for (int i = 1; i < n + 1; ++i) {
110+
result[i] = digits[i - 1];
111+
}
112+
return result;
113+
}
114+
return digits;
115+
}
116+
}
117+
```
118+
119+
### Conclusion
120+
121+
The "Plus One" problem can be efficiently solved by incrementing the large integer represented by the digits array by one. The provided solution code implements this approach in Python, C++, and Java, providing an optimal solution to the problem.

0 commit comments

Comments
 (0)