LeetCode 解題練習:Plus One

Ping-Lun Liao
Feb 23, 2024

若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。 If you like this post, please click the ads on the blog or buy me a coffee. Thank you very much.

題目原文描述 https://leetcode.com/problems/plus-one/

中文描述

給定一個用陣列 digits 表示大整數 large integer,digits[i] 代表大整數的第 i 位數之值。例如 12345 會以 digits = [1, 2, 3, 4, 5] 來表示。請將 digits 的數值加一。

範例一:

輸入 digits = [9,9,9,9]

說明:整數 9999 加一後為 10000。

範例二:

輸入 digits = [2, 2, 3, 4, 5]

說明:整數 22345 加一後為 22346。

解法:

若目前 digits[i] 為 9 ,將 digits[i] 設定為 0;否則 digits[i] 加一並回傳 digits。若每位數都是9,在最左邊補1。

Python Code

Originally published at https://yunlinsong.blogspot.com.

--

--