150強訪談簡單

加一

“加一”問題的詳細指南和 Python 實作。

問題陳述

簡單

給定一個大整數,表示為整數數組digits,其中每個digits[i]是該整數的第i位元。這些數字按從左到右的順序從最高有效位元到最低有效位元排序。大整数不包含任何前导 0。

将大整数加一并返回结果数字数组。

實作函數 plusOne(digits: list) -> list

約束條件
  • 1 <= digits.length <= 100
  • 0 <= digits[i] <= 9
  • digits does not contain any leading 0's

範例

Example 1
Input
[1,2,3]
Output
[1,2,4]
Explanation

The array represents the integer 123. Incrementing by one gives 124.

Example 2
Input
[4,3,2,1]
Output
[4,3,2,2]
Explanation

The array represents the integer 4321. Incrementing by one gives 4322.

Example 3
Input
[9]
Output
[1,0]
Explanation

The array represents the integer 9. Incrementing by one gives 10, which is [1,0].

Need a Hint?
考慮使用數學和幾何特定的資料結構,例如集合或堆疊。
Edge Cases to Watch
  • 空輸入結構
  • 單元素輸入
  • 大數值範圍

準備好解決了嗎?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

在編輯器中開啟
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推薦的 Python 資源

透過相關的互動式教學、備忘單和程式碼比較來擴展您的知識。