题目:
Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.
You may assume the integer do not contain any leading zero, except the number 0 itself.
The digits are stored such that the most significant digit is at the head of the list.
就是说一组数表示成数组形式,然后加一把她仍写成数组,如999 + 1 = 1000, [9, 9, 9]-->[1, 0, 0, 0]
程序:
class Solution:
def plusOne(self, digits):
"""
:type digits: List[int]
:rtype: List[int]
"""
N = len(digits)
i = N-1
while digits[i] == 9:
digits[i] = 0
if i == 0:
digits.insert(0,1)
return digits
else:
i -= 1
digits[i] += 1
return digits