본문 바로가기
python-algorithm

leetcode 989. Add to Array-Form of Integer

by 무적김두칠 2023. 2. 15.

https://leetcode.com/problems/add-to-array-form-of-integer/description/

 

Add to Array-Form of Integer - LeetCode

Add to Array-Form of Integer - The array-form of an integer num is an array representing its digits in left to right order. * For example, for num = 1321, the array form is [1,3,2,1]. Given num, the array-form of an integer, and an integer k, return the ar

leetcode.com

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution:
    def addToArrayForm(self, num: List[int], k: int-> List[int]:
        answer = []
        str_num = ''
 
        for number in num:
            str_num +=str(number)
 
        k += int(str_num)
 
        for number in str(k):
            answer.append(int(number))
        
        return answer
cs

num에 있는 정수들을 str 형태로 변환하고 하나로 합쳐준 다음에 k와 더합니다.
그리고 합쳐진 k를 다시 하나 하나 분리해 list에 넣어줍니다.

Convert the integers in num to str type, merge them together, and add them to k.
Then separate the combined ks one by one and put them in a list.

반응형

댓글