-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path00066-PlusOne.py
30 lines (26 loc) · 918 Bytes
/
00066-PlusOne.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# https://leetcode.com/problems/plus-one/
class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
"""
Logic: Reverse the `digits` list, if the current digit is a 9,
update `carry` to increment the next digit later
via `i` index.
If `i` exceeds the length of `digits`, append `1` to digits.
Then, reverse `digits` back to original state and return.
Time: O(n)
Space: O(1)
"""
digits = digits[::-1]
carry, i = 1, 0
while carry:
if i < len(digits):
if digits[i] == 9:
digits[i] = 0
else:
digits[i] += 1
carry = 0
else:
digits.append(1)
carry = 0
i += 1
return digits[::-1]