forked from nidhisha-shetty/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plus-one.py
29 lines (26 loc) · 958 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
27
28
29
# Given a non-empty array of decimal digits representing a non-negative integer, increment one to the integer.
The digits are stored such that the most significant digit is at the head of the list, and each element in the array contains a single digit.
You may assume the integer does not contain any leading zero, except the number 0 itself.
#Solution:
class Solution(object):
def plusOne(self, digits):
"""
:type digits: List[int]
:rtype: List[int]
"""
z=0
arr=[]
for elem in range(len(digits)):
arr.append(0)
arr[-1]=1
new_arr=digits[:]
for x in range(len(digits)-1,-1,-1):
new_arr[x]=digits[x]+arr[x]+z
if new_arr[x]>9:
new_arr[x]=0
z=1
else:
z=0
if digits[0]==9 and z==1:
new_arr.insert(0,1)
return new_arr