We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2bd1f4a commit 493899aCopy full SHA for 493899a
0066-plus-one/solution.py
@@ -1,21 +1,21 @@
1
-# Approach 1 - Schoolbook Addition with Carry
+# Approach 1: Schoolbook Addition with Carry
2
3
-# Time: O(N)
+# Time: O(n)
4
+# Space: O(1)
5
6
class Solution:
7
def plusOne(self, digits: List[int]) -> List[int]:
8
n = len(digits)
-
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
+
+ for i in range(n - 1, -1, -1):
+ if digits[i] < 9:
+ digits[i] += 1
17
return digits
18
19
- # if all digits are 9
+ # If current digit is 9, make it 0 and continue to next digit
+ digits[i] = 0
+ # If we're here, it means all digits were 9
+ # Need to add a new digit 1 at the beginning
20
return [1] + digits
21
0 commit comments