-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecompressRLEList.cpp
More file actions
43 lines (37 loc) · 919 Bytes
/
DecompressRLEList.cpp
File metadata and controls
43 lines (37 loc) · 919 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
Leetcode 1313. 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.
*/
#include <iostream>
#include <vector>
std::vector<int> decompressRLElist(std::vector<int>& nums)
{
std::vector <int> output;
for(int i = 0; i < nums.size(); i += 2)
{
int a = nums[i];
int b = nums[i+1];
for(int j = 0; j < a; j++)
output.push_back(b);
}
nums = output;
return nums;
}
int main()
{
std::vector <int> nums;
for(int i = 0; i < 4; i++)
{
nums.push_back(i + 1);
}
decompressRLElist(nums);
for(int i = 0; i < nums.size(); i++)
{
std::cout << nums[i] << " ";
}
std::cout << std::endl;
return 0;
}