close

題目:

We are given a list nums of integers representing a list compressed with run-length encoding. Consider each adjacent pair of elements [freq, val] = [nums[2*i], nums[2*i+1]] (with i >= 0). For each such pair, there are freq elements with value val concatenated in a sublist. Concatenate all the sublists from left to right to generate the decompressed list. Return the decompressed list.

解題:

提供一個List第一個數字代表數字,第二個數字代表第一個數字重複個數 根據以上方法新增一個List 使用for兩個兩個取i,使用i+1作為新增元素個數即可 append會做為list插入,本題要求為合併list故使用extend

python code:

class Solution:
    def decompressRLElist(self, nums: List[int]) -> List[int]:
        ans=[]
        for i in range(0,len(nums),2):
            ans.extend([nums[i+1]]*nums[i])
        return ans

arrow
arrow
    文章標籤
    leetcode python3 python
    全站熱搜

    低階ㄇㄋ 發表在 痞客邦 留言(0) 人氣()