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 a
a value of b
the 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 pythonic
wording.
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! ! !