def plusOne(self, digits):
"""
:type digits: List[int]
:rtype: List[int]
"""
if len(digits) == 0:
digits = [1]
elif digits[1] == 9:
digits = self.plusOne(digits[:1])
digits.extend([0])
else:
digits[1] += 1
return digits
Python simple solution using recursion

@woostundy The edge case handling an empty array of
digits
is actually unnecessary as it is given thatdigits
contains at least one number.I also opted for a recursive solution which I find more intuitive, since it corresponds to my visualization of changing the
9
to a0
in place and appending a1
to the front if necessary:class Solution: def plusOne(self, digits): if digits[1] < 9: digits[1] += 1 elif digits[1] == 9: digits[1] = 0 if len(digits) == 1: digits.insert(0, 1) else: digits[:1] = self.plusOne(digits[:1]) return digits