-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddToArrayForm.py
More file actions
24 lines (19 loc) · 821 Bytes
/
addToArrayForm.py
File metadata and controls
24 lines (19 loc) · 821 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
"""
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 array-form of the integer num + k.
Solution:
Convert list to int by first converting to string
Add the ints
Convert back to list via string
Stats:
Runtime: 512 ms, faster than 33.33% of Python3 online submissions for Add to Array-Form of Integer.
Memory Usage: 14.6 MB, less than 92.66% of Python3 online submissions for Add to Array-Form of Integer.
"""
class Solution:
def addToArrayForm(self, num: list[int], k: int) -> list[int]:
str_num = ""
for i in num:
str_num = str_num + str(i)
sum_nums = int(str_num) + k
return list(str(sum_nums))