Plus One

题目:

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.

思路:简单的大数加法

<pre name="code" class="python">class Solution:
    # @param digits, a list of integer digits
    # @return a list of integer digits
    def plusOne(self, digits):
        jinwei = 1
        res = []
        for i in xrange(len(digits)-1,-1,-1):
            if digits[i] + jinwei == 10:
                res.insert(0,0)
                jinwei = 1
            else:
                res.insert(0,digits[i] + jinwei)
                jinwei = 0
        if jinwei == 1:
            res.insert(0,1)
        return res


 
 


你可能感兴趣的:(LeetCode,python)