파비의 매일매일 공부기록

2023.02.15 Today's Challenge 본문

Problem Solving/LeetCode

2023.02.15 Today's Challenge

fabichoi 2023. 2. 15. 23:45

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

 

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

뒤에서부터 순회하면서 채워 넣으면 됨.

class Solution:
    def addToArrayForm(self, num: List[int], k: int) -> List[int]:
        for i in range(len(num)-1, -1, -1):
            k, num[i] = divmod(num[i]+k, 10)
        while k:
            k, a = divmod(k, 10)
            num = [a] + num
        return num
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

2023.02.17 Today's Challenge  (0) 2023.02.17
2023.02.16 Today's Challenge  (1) 2023.02.16
2023.02.14 Today's Challenge  (0) 2023.02.14
2023.02.13 Today's Challenge  (0) 2023.02.13
2023.02.12 Today's Challenge  (0) 2023.02.12
Comments