Plus One

Plus One

We just do add from the last digit and add carries to digits.

Code in Python:

class Solution(object):
    def plusOne(self, digits):
        """
        :type digits: List[int]
        :rtype: List[int]
        """
        carry = 1
        for i in xrange(len(digits)-1,-1,-1):
            if not carry: break
            elif digits[i]+carry == 10: digits[i], carry = 0, 1
            else: digits[i], carry = digits[i]+carry, 0
        return digits if not carry else [1]+digits

results matching ""

    No results matching ""