Simple python solution 53ms

  • 0
    class Solution:
        # @param {integer[]} digits
        # @return {integer[]}
        def plusOne(self, digits):
            digits_rev, res = list(reversed(digits)), []
            digit_sum = 1 + digits_rev[0]
            carry = digit_sum/10
            res.append(digit_sum % 10)
            for i in xrange(1, len(digits_rev)):
                digit_sum = digits_rev[i] + carry
                carry = digit_sum/10
                res.append(digit_sum % 10)
    		# check if all zero
            if all(e == 0 for e in res):
                res = list(reversed(res))
                return res
            return list(reversed(res))

    However, should we consider possible overflow exception here? Looks like testing cases does not include that.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.