Leetcode 1313: decompress coded list (ultra-detailed solution !!!)

To give you a run-length encoding compression list of integers nums.

Consider each two adjacent elements [a, b] = [nums[2*i], nums[2*i+1]](where i >= 0), each pair expressed after extracting aa value of bthe element.

Please return to the list after decompression.

Example:

输入:nums = [1,2,3,4]
输出:[2,4,4,4]

prompt:

  • 2 <= nums.length <= 100
  • nums.length % 2 == 0
  • 1 <= nums[i] <= 100

Problem-solving ideas

Means to operate in accordance with the subject.

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

More pythonicwording.

class Solution:
    def decompressRLElist(self, nums: List[int]) -> List[int]:
        return [x for a, b in zip(nums[0::2], nums[1::2]) for x in [b] * a]

Other language versions of the questions I added to my GitHub Leetcode

If you have questions, I wish to point out! ! !

Published 694 original articles · won praise 441 · views 800 000 +

Guess you like

Origin blog.csdn.net/qq_17550379/article/details/103974124