forked from gavinfish/leetcode-share
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path066 Plus One.py
More file actions
28 lines (24 loc) · 791 Bytes
/
066 Plus One.py
File metadata and controls
28 lines (24 loc) · 791 Bytes
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
'''
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.
'''
class Solution(object):
def plusOne(self, digits):
"""
:type digits: List[int]
:rtype: List[int]
"""
carry = 1
for i in range(len(digits) - 1, -1, -1):
digits[i] += carry
if digits[i] < 10:
carry = 0
break
else:
digits[i] -= 10
if carry == 1:
digits.insert(0, 1)
return digits
if __name__ == "__main__":
assert Solution().plusOne([1, 2, 3, 4, 9]) == [1, 2, 3, 5, 0]
assert Solution().plusOne([9]) == [1, 0]