66. Plus One

66. Plus One

1. 题目

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

The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.

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

Example 1:

Input: [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.

Example 2:

Input: [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.

2. 思路

此题简单,主要注意边界值。

3. 实现

class Solution(object):
    def plusOne(self, digits):
        """
        :type digits: List[int]
        :rtype: List[int]
        """
        is_greate = False 
        r = []
        if len(digits) == 0 :
            return [1]
        #digits.reverse()
        for i in range(len(digits)-1,-1,-1):
            tmp = 0
            if i == len(digits)-1 or is_greate == True :
                tmp = digits[i] + 1 
            else:
                tmp = digits[i]
            
            if tmp >=10 :
                is_greate = True
                tmp = tmp % 10  
            else:
                is_greate = False
                
            r.append(tmp)
            
        if is_greate == True: 
            r.append(1)
        
        r.reverse()

        return r
        
posted @ 2019-08-02 09:02  bush2582  阅读(98)  评论(0编辑  收藏  举报