[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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 背景 一年多以前我在知乎上答了有關LeetCode的問題, 分享了一些自己做題目的經驗。 張土汪:刷leetcod...
    土汪閱讀 12,771評論 0 33
  • 一周又over 下周準備英語課展示 今天英語競賽,題目還是做不完,題型也有變化,這次對時間的把握比去年好點,估計能...
    Breeze_yzy閱讀 136評論 0 0
  • 粗陶盆的透氣性和保濕較好 小豬臉粗陶盆很適合種植多肉植物 可以定制哦~ 當然,只要你喜歡,我們可以定制不同的款式(...
    煙窯閱讀 457評論 0 0