[easy][Array]66.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.

思路是:

通过Python中Index-1,-2,-3,来从“个位”数,模拟加法的过程。
逻辑划分是:
非首位数,分成9和不是9来处理;首位数,分成9和非9来处理。

代码是:

class Solution:
    def plusOne(self, digits):
        """
        :type digits: List[int]
        :rtype: List[int]
        """
        i = -1
        while i > - len(digits) :
            if digits[i] < 9 :
                digits[i] += 1
                return digits
            elif digits[i] == 9:
                digits[i] = 0
            i -= 1
                
        if digits[0] == 9:
            digits[0] = 0
            digits.insert(0,1)
        else:
            digits[0] += 1
        
        return digits
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,348评论 0 33
  • 一周又over 下周准备英语课展示 今天英语竞赛,题目还是做不完,题型也有变化,这次对时间的把握比去年好点,估计能...
    Breeze_yzy阅读 910评论 0 0
  • 粗陶盆的透气性和保湿较好 小猪脸粗陶盆很适合种植多肉植物 可以定制哦~ 当然,只要你喜欢,我们可以定制不同的款式(...
    烟窑阅读 3,214评论 0 0