소프트웨어에 대한 모든 것

LeetCode 풀기 - 1313. Decompress Run-Length Encoded List 본문

알고리즘/LeetCode

LeetCode 풀기 - 1313. Decompress Run-Length Encoded List

앤테바 2021. 11. 29. 21:31
반응형

1313. Decompress Run-Length Encoded List

https://leetcode.com/problems/decompress-run-length-encoded-list/

 

Decompress Run-Length Encoded List - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

문제)

솔루션1)


class Solution:
    def decompressRLElist(self, nums: List[int]) -> List[int]:
        i = 0
        res = []
        while i+1 < len(nums):
            res += [nums[i+1]] * nums[i]
            i += 2
        return res

솔루션2)

class Solution:
    def decompressRLElist(self, nums: List[int]) -> List[int]:
        res = []
        for freq, val in zip(nums[::2], nums[1::2]):
            res += [val] * freq
        return res

 

반응형
Comments