1. 问题

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

Consider each adjacent pair of elements [freq, val] = [nums[2i], nums[2i+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.

Example 1:

Input: nums = [1,2,3,4]
Output: [2,4,4,4]
Explanation: The first pair [1,2] means we have freq = 1 and val = 2 so we generate the array [2].
The second pair [3,4] means we have freq = 3 and val = 4 so we generate [4,4,4].
At the end the concatenation [2] + [4,4,4] is [2,4,4,4].

Example 2:

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

Constraints:

  • 2 <= nums.length <= 100
  • nums.length % 2 == 0
  • 1 <= nums[i] <= 100

2. 解题思路

方法1

1.新建list为temp
2.遍历数组,nums[2 * i]是限定前个数,nums[2 * i + 1]是限定数字,比如1个2,3个4,将其添加temp中
3.设定一个临时的数组,长度为temp的长度,比例数组放到result中
4.返回数组

方法2

1.定义size为0,求size的长度
2.定义result的数组,长度为size
3.定义 startIndex的初始值为0,使用Arrays.fill()填充result的数据,从 startIndex到startIndex + nums[i],value为nums[i + 1], startIndex += nums[i]
4.返回数组

方法3

方法3和2的思路是基本相同的,不同的是使用Arrays.fill()填充和两层for循环遍历得到result结果

3. 代码

代码1:

class Solution {public int[] decompressRLElist(int[] nums) {List<Integer> temp = new ArrayList();//1.新建list为tempfor(int i =0;i<nums.length/2;i++) {//2.遍历数组,nums[2 * i]是限定前个数,nums[2 * i + 1]是限定数字,比如1个2,3个4,将其添加temp中for (int j = 0; j < nums[2 * i]; j++) {temp.add(nums[2 * i + 1]);}}int[] result = new int[temp.size()];//3.设定一个临时的数组,长度为temp的长度,比例数组放到result中for(int r=0;r<temp.size();r++){result[r]=temp.get(r);}return result;//4.返回数组}
}

以下代码解题思路相同,只不过是int i = 0; i < nums.length; i += 2,换成int i =0;i<nums.length/2;i++

class Solution {public int[] decompressRLElist(int[] nums) {List<Integer> list = new ArrayList<>();for (int i = 0; i < nums.length; i += 2) {for (int j = 0; j < nums[i]; j++) {list.add(nums[i + 1]);}}int[] res = new int[list.size()];for (int i = 0; i < list.size(); i++) {res[i] = list.get(i);}return res;}
}

代码2:

class Solution {int size = 0;//1.定义size为0,求size的长度for (int i = 0; i < nums.length; i += 2) {size += nums[i];}int[] result = new int[size];//2.定义result的数组,长度为sizeint startIndex= 0;//3.定义 startIndex的初始值为0,使用Arrays.fill()填充result的数据,从 startIndex到startIndex + nums[i],value为nums[i + 1], startIndex += nums[i]for (int i = 0; i < nums.length; i += 2) {Arrays.fill(result, startIndex, startIndex + nums[i], nums[i + 1]);startIndex += nums[i];} return result;//4.返回数组}
}

代码3:

class Solution {public int[] decompressRLElist(int[] nums) {int size = 0;//1.定义size为0,求size的长度for (int i = 0; i < nums.length; i += 2) {size += nums[i];}int[] result = new int[size];//2.定义result的数组,长度为sizeint StartIndex = 0;//3.定义 startIndex的初始值为0for (int i = 0; i < nums.length; i += 2) {//4.遍历数组,StartIndex++,而不是 startIndex += nums[i];是因为在for循环i+=2下,nums[i]代表数组个数, nums[i + 1]代表数组的值for (int j = 0; j < nums[i]; j++, StartIndex++) {result[StartIndex] = nums[i + 1];}}return result;//4.返回数组}
}

LeetCode(Array)1313. Decompress Run-Length Encoded List相关推荐

  1. LeetCode刷题记录9——58. Length of Last Word(easy)

    LeetCode刷题记录9--58. Length of Last Word(easy) 目录 LeetCode刷题记录9--58. Length of Last Word(easy) 题目 语言 思 ...

  2. LeetCode 59 Spiral Matrix II(螺旋矩阵II)(Array)

    版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/5214 ...

  3. 游程编码(Run Length Coding)

    游程编码 游程编码 基本介绍 示例1 示例2 游程编码适用的场景 游程编码 游程编码(Run Length Coding,简称RLC)又称游程编码.行程长度编码.变动长度编码 等,是一种统计编码.主要 ...

  4. RLE格式标注文件转为PNG格式(Run Length Encode)

    一.什么是 RLE 格式 在机器视觉领域的深度学习中,每个数据集都有一份标注好的数据用于训练神经网络. 为了节省空间,很多数据集的标注文件使用RLE的格式,比如 kaggle 挑战赛的 Airbus ...

  5. LeetCode(持续更新)

    2021.12.6 7.整数反转 本题的困难在于判断反转后的数据是否超出范围 我的代码如下: class Solution {public int reverse(int x) {int num=0; ...

  6. LeetCode (12.整数转罗马数字)JAVA StringBuffer

    LeetCode (12.整数转罗马数字)JAVA StringBuffer 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 1 ...

  7. LeetCode(13.罗马数字转整数) JAVA Hashmap

    LeetCode(13.罗马数字转整数) JAVA Hashmap 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D ...

  8. LeetCode(1.俩数之和)JAVA

    LeetCode(1.俩数之和) 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是 ...

  9. Java 集合List、Set、HashMap操作一(Array转List、Set排序、HashMap遍历、Set遍历、List遍历、HashMap大小长度、List打乱顺序)

    数组转集合(Array转List) import java.util.*; import java.io.*;public class ArrayToCollection{public static ...

最新文章

  1. 如何找回无法运行此项请求机械硬盘的文件
  2. 倡导国际农民丰收节贸易会-万祥军:中国-中东欧谋定启动
  3. linux on zfs,在zfsonlinux中增长zpool
  4. Adwords 账户细分思路
  5. 五种方法,教你如何在Mac上查看文件完整路径
  6. Tomcat错误解决
  7. Python机器学习库sklearn几种分类算法建模可视化(实验)
  8. 《汉魏风云》1、速度与激情——无双吕布的悲喜人生
  9. hibernate的注解属性mappedBy详解
  10. iOS多张图片合成一个视频
  11. this与bind(this)
  12. IBM POWER 710(小型机) 登录AMSI
  13. 2022茶艺师(初级)考试试题模拟考试平台操作
  14. windows无法完成安装若要在此计算机,windows无法完成安装.若要在此
  15. Windows常用快捷键及结束explorer.exe出现蓝屏的办法
  16. 第四次产业革命源于人工智能(趋势解读20k字)
  17. Android简单学习使用PictureSelector框架图片选取裁剪
  18. 6款视频剪辑软件,免费又实用建议收藏!
  19. 如何做抖音自媒体?这些小技巧一定要掌握
  20. 如何删除iso镜像文件?

热门文章

  1. filebrowser安装及外网访问
  2. 如何使用笔记本共享wifi给移动设备并ss上网
  3. 员工部门mysql面试题
  4. 心田花开:人教版一年级语文《小蝌蚪找妈妈》知识点归纳
  5. 第十三周项目一(4)——验证平衡二叉树相关算法
  6. SSH2(后台管理页面+Vue+Element-UI+动态树+分页+正则表达)
  7. 竞赛无人机搭积木式编程(三)---用户自定义航点自动飞行功能(全局定位,指哪打哪)
  8. (转载)jsp的实质是什么
  9. html基础知识点_Sander_2020的博客-CSDN博客
  10. idea中查找类的快捷键