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的话,就在数组前面插入1
class Solution:
# @param digits, a list of integer digits
# @return a list of integer digits
def plusOne(self, digits):
if digits is None or len(digits) == 0: return digits
carry = 1
for i in reversed(xrange(len(digits))):
digits[i] += carry
carry = digits[i] / 10
digits[i] %= 10
if carry == 1:
digits.insert(0, 1)
return digits