66. Plus One

Posted bush2582

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
        

以上是关于66. Plus One的主要内容,如果未能解决你的问题,请参考以下文章

Office2016 的Mondo版和ProPlus版有啥区别

66_Plus-One

[leetcode][66] Plus One

算法:加一66. Plus One

66. Plus One

Leetcode-66 Plus One