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
            
    
  • 相关阅读:
    假期第五天
    假期第四天
    假期第三天
    假期第二天
    假期第一天
    《如何高效学习》读书笔记六
    十天冲刺-第八天
    十天冲刺第七天
    十天冲刺-第六天
    十天冲刺-第五天
  • 原文地址:https://www.cnblogs.com/bush2582/p/11286554.html
Copyright © 2011-2022 走看看