문제
Given anon-emptyarray 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.
풀이
class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
digits.reverse()
carry = 1
for i in range(len(digits)) :
digits[i] = digits[i] + carry
if digits[i] > 9 :
carry = 1
digits[i] = 0
else :
carry = 0
if carry == 1 :
digits.append(carry)
digits.reverse()
return digits
'알고리즘 트레이닝 > LeetCode' 카테고리의 다른 글
104. Maximum Depth of Binary Tree (0) | 2021.03.05 |
---|---|
2. Add Two Numbers (0) | 2020.05.11 |
100. Same Tree (0) | 2020.05.11 |
88. Merge Sorted Array (0) | 2020.05.11 |
21. Merge Two Sorted Lists (0) | 2020.05.09 |