[Leetcode] 66. 加一 Python3

 Python初学者,debug了半天,终于通过了,很开心,贴下代码,欢迎交流!

class Solution:
    def plusOne(self, digits):
        """
        :type digits: List[int]
        :rtype: List[int]
        """
            
        l=len(digits)   #长度
        r=len(digits)-1 #最大下标
        num = 0
        #把list处理成整数num 
        for i in range(l):
            num += (10**(r-i) )* digits[i]
        num += 1        
        li = []
        if len(str(num)) != len(str(num-1)):#考虑+1之后位数变了的情况
            l += 1
            r += 1
        #把整数num处理回list 
        for j in range(l):
            x = (num//(10**(r-j)))%10
            li.append(x)         
        return li
            

猜你喜欢

转载自blog.csdn.net/niceHou666/article/details/81218963