Wednesday, May 21, 2014

Leetcode (Python): Plus One

Given a number represented as an array of digits, plus one to the number.

Solution:

class Solution:
    # @param digits, a list of integer digits
    # @return a list of integer digits
    def plusOne(self, digits):
        carryOn = True
        for i in range(len(digits)-1,-1,-1):
            if carryOn == False:
                break
            digits[i] += 1
            carryOn = digits[i] >9
            digits[i] %= 10
        if carryOn:
            digits.insert(0,1)
        return digits

No comments :

Post a Comment