[
math
]
BinarySearch 0219 Add One to List
Problem statement
https://binarysearch.com/problems/Add-One-to-List/
Solution
Equal to Leetcode 0066. Plus One.
Complexity
It is O(n)
for time and O(1)
for additional space.
Code
class Solution:
def solve(self, digits):
digits = [0] + digits
end = len(digits) - 1
while digits[end] == 9:
end -= 1
digits[end] += 1
digits[end+1:] = [0] * (len(digits)-1-end)
return digits if digits[0] != 0 else digits[1:]