Endless solution algorithm of compression coding list

To give you a run-length encoding compression list of integers nums.

Consider each two adjacent elements [a, b] = [the nums [2 I], the nums [2 I +. 1]] (where i> = 0), each pair have indicated a value of b after a decompression element.

Please return to the list after decompression.

Example:

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

prompt:

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

Ideas:

Compression encoding incoming cycle of violence, step 2, as are the even index value, we first need to get the number of value, i.e. odd index. The result after passing the ArrayList decompressed, converted to an array of return.

answer:

class Solution {
    public int[] decompressRLElist(int[] nums) {
        ArrayList resList=new ArrayList();
        for(int i=0; i<nums.length; i+=2){
            for(int j=0; j<nums[i];j++){
                resList.add(nums[i+1]);
            }
        }
        int[] res=new int[resList.size()];
        for(int i=0;i<resList.size();i++){
            res[i]=(int)resList.get(i);
        }
        return res;
    }
}
Published 129 original articles · won praise 239 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_33709508/article/details/103942873