class Solution:
"""
@param: digits: a number represented as an array of digits
@return: the result
"""
def plusOne(self, digits):
# write your code here
flag = 0
a = [-1] * (len(digits) + 1)
# 处理最后一位数 +1
a[-1] = digits[-1] + 1
if a[-1] == 10:
a[-1] = 0
flag = 1
else:
flag = 0
# 处理中间的数
for i in range(-2, -len(a), -1):
a[i] = digits[i] + flag
flag = 0
if a[i] == 10:
a[i] = 0
flag = 1
# 处理最前面的数
if a[1] == 0:
a[0] = 1
else:
del a[0]
return a
lintcode : 加一
最新推荐文章于 2018-06-13 19:14:58 发布