- Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplus_one.py
26 lines (20 loc) · 839 Bytes
/
plus_one.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
classSolution:
defplusOne(self, digits: List[int]) ->List[int]:
# plus one
carry_digit=0
digits[len(digits)-1] =digits[len(digits)-1] +1
# for loop (start from the last one)
forindexinrange( len(digits)-1, -1, -1):
# add the carry_digit
digits[index] =digits[index] +carry_digit
ifdigits[index] ==10:
digits[index] =0
carry_digit=1
else:
digits[index] =digits[index]
carry_digit=0
# handle the edge case:
# append integer to the beginning of the list
ifcarry_digit==1:
digits.insert(0, 1) # insert(index, value)
returndigits