-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplus_one.py
More file actions
38 lines (29 loc) · 882 Bytes
/
Copy pathplus_one.py
File metadata and controls
38 lines (29 loc) · 882 Bytes
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
31
32
33
34
35
36
37
38
from typing import List
class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
list_len = len(digits) - 1
i = list_len
nine_found = False
while i >= 0:
n = digits[i]
if n == 9 and i == list_len:
nine_found = True
digits[i] = 0
elif n == 9 and nine_found:
digits[i] = 0
elif nine_found:
nine_found = False
digits[i] = n + 1
elif i == list_len:
digits[i] = n + 1
i -= 1
if nine_found:
return [1] + digits
return digits
sol = Solution()
print(sol.plusOne([1, 2, 3]))
print(sol.plusOne([9]))
print(sol.plusOne([1, 0, 9]))
print(sol.plusOne([1, 0, 9]))
print(sol.plusOne([1, 1, 1, 1, 1, 1, 1]))
print(sol.plusOne([9, 9, 8, 9, 9, 9]))