Leetcode 1313. 解压缩编码列表

简介: Leetcode 1313. 解压缩编码列表

Leetcode 1313. 解压缩编码列表


Table of Contents

中文版:

英文版:

My answer:

学习:

中文版:

给你一个以行程长度编码压缩的整数列表 nums 。

考虑每相邻两个元素 [a, b] = [nums[2*i], nums[2*i+1]] (其中 i >= 0 ),每一对都表示解压后有 a 个值为 b 的元素。

请你返回解压后的列表。

示例:

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

提示:

2 <= nums.length <= 100

nums.length % 2 == 0

1 <= nums[i] <= 100

英文版:

5143. Decompress Run-Length Encoded List

We are given a list nums of integers representing a list compressed with run-length encoding.

Consider each adjacent pair of elements [a, b] = [nums[2*i], nums[2*i+1]] (with i >= 0).  For each such pair, there are a elements with value b in the decompressed list.

Return the decompressed list.

Example 1:

Input: nums = [1,2,3,4] Output: [2,4,4,4]

Constraints:

2 <= nums.length <= 100

nums.length % 2 == 0

1 <= nums[i] <= 100

 

My answer:

class Solution:
    def decompressRLElist(self, nums: List[int]) -> List[int]:
        result = []
        for i in range(len(nums)):
            if i % 2 == 0:
                a = nums[i]
            else:
                b = nums[i]
                result += a * [b]
        return result

学习:

1、a * [b] 表示生成列表,列表中有 a 个 b。

2、result += a * [b],可理解为列表的拼接,注意与 append() 函数的区别。

相关文章
|
9月前
leetcode-89:格雷编码
leetcode-89:格雷编码
64 0
|
9月前
|
存储 算法 安全
【刷题】 leetcode 面试题 01.06 字符串压缩
来看效果: 非常好!!!过啦!!!
83 5
【刷题】 leetcode 面试题 01.06 字符串压缩
|
人工智能 BI 索引
【Leetcode -598.范围求和Ⅱ -599.两个列表的最小索引总和】
【Leetcode -598.范围求和Ⅱ -599.两个列表的最小索引总和】
56 0
LeetCode-393 UTF-8编码验证
LeetCode-393 UTF-8编码验证
|
C++
LeetCode | 双法妙解压缩字符串【遍历统计 + 双指针】
LeetCode | 双法妙解压缩字符串【遍历统计 + 双指针】
85 0
|
程序员
【Leetcode】面试题 01.06. 字符串压缩、面试题 05.07. 配对交换
目录 面试题 01.06. 字符串压缩: 面试题 05.07. 配对交换
47 0
|
算法 测试技术 Android开发
LeetCode 周赛上分之旅 #40 结合特征压缩的数位 DP 问题
学习数据结构与算法的关键在于掌握问题背后的算法思维框架,你的思考越抽象,它能覆盖的问题域就越广,理解难度也更复杂。在这个专栏里,小彭与你分享每场 LeetCode 周赛的解题报告,一起体会上分之旅。
77 0
|
算法
leetcode443–压缩字符串(双指针/简单)
leetcode443–压缩字符串(双指针/简单)
|
索引 Python
LeetCode 599. 两个列表的最小索引总和
假设Andy和Doris想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示。
108 0
|
索引 Python
LeetCode 820. 单词的压缩编码 Short Encoding of Words
LeetCode 820. 单词的压缩编码 Short Encoding of Words