題目
原題頁面:https://leetcode.com/problems/plus-one/
題目型別:Array, Math
難度評價:Easy
類似題目:(M) Multiply Strings, (E) Add Binary
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 the head of the list.
分析
用一個int陣列代表一個數值,最左邊是最高位,現要求加上1,同樣地返回一個int陣列代表計算結果。
非常簡單的模擬加法,注意進位問題,如果是99,999這種,最後加上1後還要在陣列最左邊插入一個元素1。
無論最高位有沒有發生進位,空間複雜度都是O(1)。時間複雜度是O(N)。
程式碼
1 | class Solution: |
