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.
题意描述得有点让人难理解,举例说明nums=[1,2,3,4],那么但是1个2和3个4这两个子数组连接得到[2], [4, 4, 4] = [2, 4, 4, 4]
class Solution(object): def decompressRLElist(self, nums): """ :type nums: List[int] :rtype: List[int] """ ans = [] for i in range(0, len(nums), 2): ans += [nums[i + 1]] * nums[i] return ans