We are given a list
Consider each adjacent pair of elements
Return the decompressed list.
nums of integers representing a list compressed with run-length encoding.Consider each adjacent pair of elements
[freq, val] = [nums[2*i], nums[2*i+1]] (with i >= 0). For each such pair, there are freq elements with value val concatenated in a sublist. Concatenate all the sublists from left to right to generate the decompressed list.Return the decompressed list.
Input: nums = [1,2,3,4] Output: [2,4,4,4]
Solution in C++:
vector<int> decompressRLElist(vector<int>& nums) {
vector<int> ret;
for(int iter = 0; iter < nums.size();){
int i = 0;
while(i < nums.at(iter)){
++i;
ret.push_back(nums.at(iter + 1));
}
iter += 2;
}
return ret;
}
Comments
Post a Comment