Skip to content

Commit 493899a

Browse files
committed
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 17.7 MB (59.83%)
1 parent 2bd1f4a commit 493899a

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

0066-plus-one/solution.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
# Approach 1 - Schoolbook Addition with Carry
1+
# Approach 1: Schoolbook Addition with Carry
22

3-
# Time: O(N)
3+
# Time: O(n)
4+
# Space: O(1)
45

56
class Solution:
67
def plusOne(self, digits: List[int]) -> List[int]:
78
n = len(digits)
8-
9-
for i in range(n):
10-
idx = n - 1 - i
11-
12-
if digits[idx] == 9:
13-
digits[idx] = 0
14-
15-
else:
16-
digits[idx] += 1
9+
10+
for i in range(n - 1, -1, -1):
11+
if digits[i] < 9:
12+
digits[i] += 1
1713
return digits
18-
19-
# if all digits are 9
14+
15+
# If current digit is 9, make it 0 and continue to next digit
16+
digits[i] = 0
17+
18+
# If we're here, it means all digits were 9
19+
# Need to add a new digit 1 at the beginning
2020
return [1] + digits
2121

0 commit comments

Comments
 (0)