1. 程式人生 > >LeetCode OJ 系列之66 Plus One --Python

LeetCode OJ 系列之66 Plus One --Python

Problem:

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.

Answer:
class Solution(object):
    def plusOne(self, digits):
        """
        :type digits: List[int]
        :rtype: List[int]
        """
        carry_bit = 1
        for i in range(len(digits)-1,-1,-1):
            if digits[i]+carry_bit>9:
                digits[i] = 0
            else : 
                digits[i] = digits[i]+carry_bit
                carry_bit = 0
        if carry_bit == 1:
            digits.insert(0,1)
        return digits