Given a non-empty array of digits representing a non-negative integer, plus one to the integer.
The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.
You may assume the integer does not contain any leading zero, except the number 0 itself.
Example 1:
Input: [1,2,3] Output: [1,2,4] Explanation: The array represents the integer 123. |
Example 2:
Input: [4,3,2,1] Output: [4,3,2,2] Explanation: The array represents the integer 4321. |
注意:本题主要是模拟加法运算过程,c为进位。特殊的当数组为[9, 9],最后的输出应该是[1, 0, 0],需要自己重新定义一个新数组封装最后的数据
class Solution(object):
def plusOne(self, digits):
"""
:type digits: List[int]
:rtype: List[int]
"""
c = 1
for i in range(len(digits) - 1, -1, -1):
if digits[i] + c >= 10:
c = 1
digits[i] = 0
else:
digits[i] += c
c = 0
break
if c == 1:
num = [1]
for e in digits:
num.append(e)
return num
return digits