1. 程式人生 > >LeetCode(Python版)——66. Plus One

LeetCode(Python版)——66. Plus One

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.

注意:本題主要是模擬加法運算過程,c為進位。特殊的當陣列為[9, 9],最後的輸出應該是[1, 0, 0],需要自己重新定義一個新陣列封裝最後的資料

class Solution(object):
    def plusOne(self, digits):
        """
        :type digits: List[int]
        :rtype: List[int]
        """
        c = 1
        for i in range(len(digits) - 1, -1, -1):
            if digits[i] + c >= 10:
                c = 1
                digits[i] = 0
            else:
                digits[i] += c
                c = 0
                break
        if c == 1:
            num = [1]
            for e in digits:
                num.append(e)
            return num
        
        return digits

相關推薦

LeetCode(Python)——66. Plus One

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

LeetCode OJ 系列之66 Plus One --Python

Problem: Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the m

LeetCode 66. Plus One(加1)

class 數字 public store res rest self present [0 Given a non-negative integer represented as a non-empty array of digits, plus one to the i

[leetcode] 66. Plus One

num solution 水題 strong can leading ger ica ati Given a non-negative integer represented as a non-empty array of digits, plus one to th

[leetcode] 66. Plus One 解題報告

ati pan can turn color con length empty leet Given a non-negative integer represented as a non-empty array of digits, plus one to the int

leetcode 66. Plus One

insert most tin integer output assume AS bsp else Given a non-empty array of digits representing a non-negative integer, plus one to th

[leetcode][66] Plus One

digits plus one inpu self. represent sum pub The hat 66. Plus One Given a non-empty array of digits representing a non-negative integer,

[leetcode]66.Plus One

nts sin ref .cn 沒有 ica ret n-n out 題目 Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The

66. Plus Onepython+cpp)

題目: Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The digits are stored such that the mo

#Leetcode# 66. Plus One

https://leetcode.com/problems/plus-one/   Given a non-empty array of digits representing a non-negative integer, plus one to the inte

LeetCode#66: Plus One

class Solution { public int[] plusOne(int[] digits) { int n = digits.length - 1;

leetcode 66 Plus One(加一運算)

題目要求 給定一個非空的數字陣列,該陣列表示一個非負整數,要求對這個非負整數加上1。返回操作後的陣列。 示例 輸入:[1,2,3] 表示123, 輸出[1,2,4] (123+1=124) 解題思路 因為題目已經說明不考慮負數的情況,所以對於任意非負數進行加

LeetCode--66. Plus One

題目連結:https://leetcode.com/problems/plus-one/ 該問題有點類似於連結串列的加一:2. Add Two Numbers和二進位制表達的字串加1的問題:67. Add Binary,比較簡單,就是設定一個邏輯型變數的進位標誌,程式碼如下:

[leetcode]66. Plus One

MY DUMMY SOLUTION class Solution { public int[] plusOne(int[] digits) { int c=0; digits[digits.length-1]=digits[digits.lengt

Leetcode 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

LeetCode --- 66. Plus One

Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at t

Leetcode 66. Plus One 加一! 解題報告

1 解題思想 這道題,就是做一個加法,加1的操作而已。。 要點: 臨時陣列需要開一個比輸入長1的,防止最高位進位。 低位加一,然後處理進位,如有進位,那麼高一位執行+1操作,如此往復迭代。 2 原題 Given a non-negative numb

66. Plus One

push ber exce urn sign cto plus one sent dig Given a non-negative integer represented as a non-empty array of digits, plus one to the int

66. Plus One - Easy

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 si

[leetecode ] [C++]66.Plus One

題意:用數列模擬整數加法,注意9+1後的進位和999…9之類的數會增加一位 class Solution { public: vector<int> plusOne(vector<int>& digits) {