题意
给你一个以行程长度编码压缩的整数列表 nums 。
考虑每对相邻的两个元素 [freq, val] = [nums[2i], nums[2i+1]] (其中 i >= 0 ),每一对都表示解压后子列表中有 freq 个值为 val 的元素,你需要从左到右连接所有子列表以生成解压后的列表。
请你返回解压后的列表。
示例
示例:
输入:nums = [1,2,3,4]
输出:[2,4,4,4]
解释:第一对 [1,2] 代表着 2 的出现频次为 1,所以生成数组 [2]。
第二对 [3,4] 代表着 4 的出现频次为 3,所以生成数组 [4,4,4]。
最后将它们串联到一起 [2] + [4,4,4] = [2,4,4,4]。
示例 2:
输入:nums = [1,1,2,3]
输出:[1,3,3]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/decompress-run-length-encoded-list
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解答与思路
class Solution {
public int[] decompressRLElist(int[] nums) {
// 新数组长度
int newArrayLength = 0;
for(int i = 0; i<nums.length;i += 2){
newArrayLength += nums[i];
}
// 开始组装
// 新数组的下标
int tempIndex = 0;
int[] newArray = new int[newArrayLength];
// 遍历老数组用于拆解
for(int i = 0; i<nums.length;i += 2){
// 第一位是value的出现次数 先暂存 用于遍历
int currentTimes = nums[i];
while(currentTimes>0){
// 取 value
newArray[tempIndex] = nums[i+1];
// 新数组下标后移
tempIndex++;
// value 出现一次 少循环一次
currentTimes--;
}
}
return newArray;
}
}