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.
题意:一个整数按位存储于一个数组中,排列顺序为:最高位在digits[0] ,最低位在digits[n-1],
例如:10,存储为:digits[0]=1; digits[1]=0;
class Solution(object):
def plusOne(self, digits):
"""
:type digits: List[int]
:rtype: List[int]
"""
length = len(digits)
digits.reverse()
c = 1
for i in range(length):
if digits[i] == 9 and c == 1:
digits[i] = 0
c = 1
elif digits[i] != 9 and c == 1:
digits[i] += 1
c = 0
if c == 1:
digits.append(1)
digits.reverse()
return digits