LeetCode 989. Integer addition in the form of an array

LeetCode 989. Integer addition in the form of an array

I don't know where I am going, but I am already on my way!
Time is hurried, although I have never met, but I met Yusi, it is really a great fate, thank you for your visit!
  • Question :
    For non-negative integer X, the array form of X is an array formed by each digit in the order from left to right. For example, if X = 1231, then its array form is [1,2,3,1].
    Given the array form A of the non-negative integer X, return the array form of the integer X+K.
  • Example :
示例 1 :
输入:A = [1,2,0,0], K = 34
输出:[1,2,3,4]
解释:1200 + 34 = 1234
示例 2 :
输入:A = [2,7,4], K = 181
输出:[4,5,5]
解释:274 + 181 = 455
示例 3 :
输入:A = [2,1,5], K = 806
输出:[1,0,2,1]
解释:215 + 806 = 1021
示例 4 :
输入:A = [9,9,9,9,9,9,9,9,9,9], K = 1
输出:[1,0,0,0,0,0,0,0,0,0,0]
解释:9999999999 + 1 = 10000000000
  • Code:
class Solution:
    def addToArrayForm(self, A: List[int], K: int) -> List[int]:
        s = ''
        a = []
        for i in range(len(A)):
            s += str(A[i])
        q = int(s) + K
        for i in str(q):
            a.append(int(i))
        return a
# 执行用时 : 280 ms, 在Add to Array-Form of Integer的Python3提交中击败了43.75% 的用户
# 内存消耗 : 13.5 MB, 在Add to Array-Form of Integer的Python3提交中击败了100.00% 的用户
  • Algorithm description:
    Convert an array to a string, convert a string to an integer, perform an addition operation, then convert the addition result into a string, and then output the string as an array.

Guess you like

Origin blog.csdn.net/qq_34331113/article/details/106626738