附leetcode链接:https://leetcode.com/problems/decompress-run-length-encoded-list/
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[2i],nums[2i+1]](with i >= 0).For each such pair, there are a elements with value b in the decompressed list.
public int[] decompressRLElist(int[] nums) {
ArrayList<Integer> a = new ArrayList<Integer>();
int t = 0;
for(int i = 0;i<nums.length;i = i + 2) {
for(int j = 0;j<nums[i];j++){
a.add(nums[i+1]);
}
}
int b[] = new int[a.size()];
for (int i = 0; i < a.size();i++) {
b[i] = a.get(i);
}
return b;
}
小结:当前解法不是最快的,有待进一步探索;
属于对【整形数组】的处理;
用到了双重循环;
数组初始化的时候必须指定长度,这里用到了ArrayList,长度动态分配;
用一个for循环转换成普通的数组;
list常用的add、get、size方法:https://www.runoob.com/java/java-collections.html;