-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlusOne.py
More file actions
25 lines (23 loc) · 767 Bytes
/
PlusOne.py
File metadata and controls
25 lines (23 loc) · 767 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
# Question link - https://leetcode.com/problems/plus-one/?envType=study-plan-v2&envId=top-interview-150
class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
# Reverse the digits
digits = digits[::-1]
# Set the one , ith pointers
one , i = 1 , 0
# Traverse the array:
while one:
if i < len(digits):
# Special case 9
if digits[i] == 9:
digits[i] = 0
else:
digits[i] += 1
one = 0
else:
# When i > len(digits)
digits.append(1)
one = 0
i += 1
# Return the digits in reverse
return digits[::-1]