-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplusone.py
More file actions
39 lines (31 loc) · 985 Bytes
/
plusone.py
File metadata and controls
39 lines (31 loc) · 985 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
39
from typing import List
class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
return self.plusOne_helper(digits, carry=1, idx=len(digits)-1)
def plusOne_helper(self, digits, carry, idx):
val = digits[idx] + carry
new_digit = val % 10
new_carry = val // 10
digits[idx] = new_digit
if idx == 0:
if new_carry == 0:
return digits
else:
return [new_carry] + digits
else:
if new_carry == 0:
return digits
else:
return self.plusOne_helper(digits, new_carry, idx-1)
def test1():
Input = [1, 2, 3]
Output = [1, 2, 4]
assert Solution().plusOne(Input) == Output
def test2():
Input = [4, 3, 2, 1]
Output = [4, 3, 2, 2]
assert Solution().plusOne(Input) == Output
def test3():
Input = [9, 9, 9, 9]
Output = [1, 0, 0, 0, 0]
assert Solution().plusOne(Input) == Output