zoukankan      html  css  js  c++  java
  • 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
            
    
  • 相关阅读:
    在Eclipse中设置中文JavaDOC
    买车,给点建议和意见
    父亲节
    JSP文件上传

    昨天我生日
    换皮了
    西安夕阳
    WinForms中只能输入数字的文本框
    使用GoogleCode SVN服务
  • 原文地址:https://www.cnblogs.com/bush2582/p/11286554.html
Copyright © 2011-2022 走看看