-
Notifications
You must be signed in to change notification settings - Fork 43
/
plus-one.py
131 lines (117 loc) · 3.2 KB
/
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
"""
# https://github.com/kamyu104/LeetCode/blob/master/Python/plus-one.py
Given a non-empty array of digits representing a non-negative integer, plus 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 contain a single digit.
You may assume the integer does not contain any leading zero, except the number 0 itself.
Example 1:
Input: [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
Example 2:
Input: [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.
"""
# V0
# NOTE : Notice the index of inverse loop : range(len(a)-1, -1, -1)
# a = ['a', 'b', 'c']
# for i in range(len(a)-1, -1, -1):
# print (a[i])
# c
# b
# a
class Solution(object):
def plusOne(self, digits):
"""
:type digits: List[int]
:rtype: List[int]
"""
plus = 1
for i in range(len(digits)-1, -1, -1):
if digits[i] + plus > 9:
digits[i] = 0
plus = 1
else:
digits[i] = digits[i] + plus
plus = 0
if plus == 1:
digits.insert(0, 1)
return digits
# V0'
# IDEA : array -> string -> int -> string
class Solution:
def plusOne(self, digits):
digits_ = [str(i) for i in digits]
d = int(''.join(digits_))
d += 1
return list(str(d))
# V1
# https://blog.csdn.net/coder_orz/article/details/51583916
class Solution(object):
def plusOne(self, digits):
"""
:type digits: List[int]
:rtype: List[int]
"""
plus = 1
for i in range(len(digits)-1, -1, -1):
if digits[i] + plus > 9:
digits[i] = 0
plus = 1
else:
digits[i] = digits[i] + plus
plus = 0
if plus == 1:
digits.insert(0, 1)
return digits
### Test case : dev
# V1'
# https://blog.csdn.net/coder_orz/article/details/51583916
class Solution(object):
def plusOne(self, digits):
"""
:type digits: List[int]
:rtype: List[int]
"""
for i in range(len(digits)-1, -1, -1):
if digits[i] < 9:
digits[i] = digits[i] + 1
return digits
else:
digits[i] = 0
digits.insert(0, 1)
return digits
# V2
# Time: O(n)
# Space: O(1)
class Solution(object):
def plusOne(self, digits):
"""
:type digits: List[int]
:rtype: List[int]
"""
for i in reversed(range(len(digits))):
if digits[i] == 9:
digits[i] = 0
else:
digits[i] += 1
return digits
digits[0] = 1
digits.append(0)
return digits
# Time: O(n)
# Space: O(n)
class Solution2(object):
def plusOne(self, digits):
"""
:type digits: List[int]
:rtype: List[int]
"""
result = digits[::-1]
carry = 1
for i in range(len(result)):
result[i] += carry
carry, result[i] = divmod(result[i], 10)
if carry:
result.append(carry)
return result[::-1]