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.
반응형
'python-algorithm' 카테고리의 다른 글
leetcode 206. Reverse Linked List (0) | 2023.02.17 |
---|---|
백준 9772 Quadrants (0) | 2023.02.17 |
leetcode 21. Merge Two Sorted Lists (0) | 2023.02.13 |
leetcode 1523. Count Odd Numbers in an Interval Range (0) | 2023.02.13 |
leetcode 234. Palindrome Linked List (0) | 2023.02.11 |
댓글