-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy path065 Plus One.py
More file actions
57 lines (46 loc) · 1.38 KB
/
065 Plus One.py
File metadata and controls
57 lines (46 loc) · 1.38 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
46
47
48
49
50
51
52
53
54
55
56
57
"""
Given a non-negative number represented as an array of digits, plus one to the number.
The digits are stored such that the most significant digit is at the head of the list.
"""
__author__ = 'Danyang'
class Solution(object):
def plusOne(self, digits):
"""
Math
Basics of all other questions like adding, multiplying.
:param digits: a list of integer digits
:return: a list of integer digits
"""
for i in xrange(len(digits)-1, -1, -1):
digits[i] += 1
if digits[i] < 10:
return digits
else:
digits[i] -= 10
# MSB
digits.insert(0, 1)
return digits
def plusOne(self, digits):
"""
Good habit to reverse it first
:param digits:
:return:
"""
digits.reverse()
digits[0] += 1
carry = 0
for i in xrange(len(digits)): # for ind, val in enumerate(digits):
digits[i] += carry
if digits[i] > 9:
digits[i] -= 10
carry = 1
else:
carry = 0
break
if carry:
digits.append(1)
digits.reverse()
return digits
if __name__ == "__main__":
digits = [9]
assert Solution().plusOne(digits) == [1, 0]