Skip to content

Commit 1706922

Browse files
committed
[LeetCode Sync] Runtime - 0 ms (100.00%), Memory - 18 MB (8.76%)
1 parent ef8430e commit 1706922

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

Solutions/0066-plus-one/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<p>You are given a <strong>large integer</strong> represented as an integer array <code>digits</code>, where each <code>digits[i]</code> is the <code>i<sup>th</sup></code> 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 <code>0</code>&#39;s.</p>
2+
3+
<p>Increment the large integer by one and return <em>the resulting array of digits</em>.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
8+
<pre>
9+
<strong>Input:</strong> digits = [1,2,3]
10+
<strong>Output:</strong> [1,2,4]
11+
<strong>Explanation:</strong> The array represents the integer 123.
12+
Incrementing by one gives 123 + 1 = 124.
13+
Thus, the result should be [1,2,4].
14+
</pre>
15+
16+
<p><strong class="example">Example 2:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> digits = [4,3,2,1]
20+
<strong>Output:</strong> [4,3,2,2]
21+
<strong>Explanation:</strong> The array represents the integer 4321.
22+
Incrementing by one gives 4321 + 1 = 4322.
23+
Thus, the result should be [4,3,2,2].
24+
</pre>
25+
26+
<p><strong class="example">Example 3:</strong></p>
27+
28+
<pre>
29+
<strong>Input:</strong> digits = [9]
30+
<strong>Output:</strong> [1,0]
31+
<strong>Explanation:</strong> The array represents the integer 9.
32+
Incrementing by one gives 9 + 1 = 10.
33+
Thus, the result should be [1,0].
34+
</pre>
35+
36+
<p>&nbsp;</p>
37+
<p><strong>Constraints:</strong></p>
38+
39+
<ul>
40+
<li><code>1 &lt;= digits.length &lt;= 100</code></li>
41+
<li><code>0 &lt;= digits[i] &lt;= 9</code></li>
42+
<li><code>digits</code> does not contain any leading <code>0</code>&#39;s.</li>
43+
</ul>
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+
digits[i] += 1
6+
digits[i] %= 10
7+
if digits[i] != 0:
8+
return digits
9+
return [1] + digits

0 commit comments

Comments
 (0)