-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplusOne.py
More file actions
45 lines (34 loc) · 1.23 KB
/
plusOne.py
File metadata and controls
45 lines (34 loc) · 1.23 KB
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
40
41
42
43
44
45
"""
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.
"""
class Solution:
def plusOne(self, digits: list[int]) -> list[int]:
# converting the int list into a string, then into an integer for easy addition.
# Then converting the result back into a list of integers
digits = [str(i) for i in digits]
digits = int(''.join(digits))
digits = digits + 1
return [int(x) for x in str(digits)]
"""
The solution above is a bit crude...
Converting inputs to different formats et al.
Below is a better implementation using an iterative solution
"""
def plusOne(input_array):
carry = 1
n = len(input_array)
new_array = [0] * n
for i in range(n-1, -1, -1):
sum = input_array[i] + carry
if sum == 10:
carry = 1
else:
carry = 0
new_array[i] = sum % 10
if carry == 1:
new_array = [0] * (n+1)
new_array[0] = 1
return new_array