当前位置:网站首页>Force buckle 989 Integer addition in array form

Force buckle 989 Integer addition in array form

2022-07-07 20:06:00 Tomorrowave

989. The addition of integers in the form of arrays

The integer Array form num Is an array of numbers in left to right order .

 for example , about  num = 1321 , The array form is  [1,3,2,1] .

Given num , The integer Array form , And integer k , return Integers num + k Of Array form .

Knowledge points involved

String conversion

class Solution:
    def addToArrayForm(self, A: List[int], K: int) -> List[int]:
        K = list(map(int,str(K)))
        res = []
        i,j = len(A)-1,len(K)-1
        carry = 0

        while i >= 0 and j >= 0:
            res.append(A[i] + K[j] + carry)
            res[-1],carry = res[-1] % 10, res[-1] // 10
            i -= 1
            j -= 1
        while i >= 0:
            res.append(A[i] + carry)
            res[-1],carry = res[-1] % 10, res[-1] // 10
            i -= 1
        while j >= 0:
            res.append(K[j] + carry)
            res[-1],carry = res[-1] % 10, res[-1] // 10
            j -= 1

        if carry:
            res.append(1)

        return res[::-1]

map() function

in other words ,map You can map the array of this question

del square(x):
    return x ** 2
 
map(square,[1,2,3,4])#[1,4,9,16] 
map(int,'1234')      #[1,2,3,4]
class Solution:
    def addToArrayForm(self, num: List[int], k: int) -> List[int]:
        return [int(i) for i in str((int(str(num)[1:-1:3]))+k)]
or:
        return list(map(int,str(int(''.join(str(num))) + K)))
原网站

版权声明
本文为[Tomorrowave]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071753356602.html

随机推荐