mycode

主要在计算商和余数的时候一定要用还没更新的商和余数哦

class Solution(object):
    def plusOne(self, digits):
        """
        :type digits: List[int]
        :rtype: List[int]
        """
        add = 1
        for i in range(len(digits)-1,-1,-1):        
            digits[i],add= (digits[i] + add) % 10,(digits[i] + add) // 10      
        if add :
             digits.insert(0,add)
        return digits
            

 

参考:

def plusOne(self, digits):
    j = len(digits) - 1
    cout = 1  
    while(j >= 0 and cout):
        digits[j] += cout
        cout, digits[j] = digits[j] // 10, digits[j] % 10
        j -= 1
    return digits if not cout else [cout] + digits