[코테 풀이] Decompress Run-Length Encoded List

시내·2024년 6월 12일

Q_1313) Decompress Run-Length Encoded List

출처 : https://leetcode.com/problems/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 [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.

class Solution {
    public int[] decompressRLElist(int[] nums) {
        List<Integer> list = new ArrayList<>();
        int ind = 0;
        for (int i = 0; i < nums.length / 2; i++) {
            for (int j = 0; j < nums[2 * i]; j++) {
                list.add(nums[2 * i + 1]);
            }
        }
        int[] res = new int[list.size()];
        for (int l : list) {
            res[ind++] = l;
        }
        return res;
    }
}
profile
contact 📨 ksw08215@gmail.com

0개의 댓글