-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlusOne.py
More file actions
32 lines (30 loc) · 893 Bytes
/
Copy pathPlusOne.py
File metadata and controls
32 lines (30 loc) · 893 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
29
30
31
32
# coding: utf-8
"""
@Project: leetcode
@file: PlusOne.py
@author: AC
@time: 2016/5/9 0:21
@Description: 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.
大数加法[1,2,3,4]+1=[1,2,3,5] [1,2,3,9]+1=[1,2,4,0]
"""
class Solution(object):
def plusOne(self, digits):
"""
:type digits: List[int]
:rtype: List[int]
"""
result = []
jinwei = 1
for i in xrange(len(digits)-1, -1, -1):
temp = digits[i]+jinwei
if temp >= 10:
result.append(temp-10)
jinwei = 1
else:
result.append(temp)
jinwei = 0
if jinwei > 0:
result.append(jinwei)
result.reverse()
return result