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
Solution: Simulation
Time complexity: O(sum(n_i))
Space complexity: O(sum(n_i)) or O(1)
C++
1 2 3 4 5 6 7 8 9 10 11 |
// Author: Huahua class Solution { public: vector<int> decompressRLElist(vector<int>& nums) { vector<int> ans; for (int i = 0; i < nums.size(); i += 2) for (int j = 0; j < nums[i]; ++j) ans.push_back(nums[i + 1]); return ans; } }; |
请尊重作者的劳动成果,转载请注明出处!花花保留对文章/视频的所有权利。
如果您喜欢这篇文章/视频,欢迎您捐赠花花。
If you like my articles / videos, donations are welcome.
Be First to Comment