LC 0066 [E] Plus One - ALawliet/algorithms GitHub Wiki

class Solution:
    def plusOne(self, digits: List[int]) -> List[int]:
        n = len(digits)

        # move along the input array starting from the end
        for l in range(n):
            r = n - 1 - l
            # set all the nines at the end of array to zeros
            if digits[r] == 9:
                digits[r] = 0
            # here we have the rightmost not-nine
            else:
                # increase this rightmost not-nine by 1
                digits[r] += 1
                # and the job is done
                return digits

        # we're here because all the digits are nines
        return [1] + digits