LeetCode - Plus one

Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.

You may assume the integer do not contain any leading zero, except the number 0 itself.

The digits are stored such that the most significant digit is at the head of the list.

===================================================================

class Solution(object):
    def plusOne(self, digits):
        """
        :type digits: List[int]
        :rtype: List[int]
        """
        n = len(digits)
        for i in range(n):
            digits[-i-1] = digits[-i-1] + 1
            if digits[-i-1] == 10:
                digits[-i-1] = 0
                if i==n-1:
                    digits.insert(0,1)
                    return digits
            else:
                return digits
       
    
test = Solution()
a = [1,9,9]
print test.plusOne(a)
posted @ 2017-12-12 14:20  YWU  阅读(85)  评论(0)    收藏  举报