leetcode:1313. 解压缩编码列表

题目来源

题目详情

在这里插入图片描述

在这里插入图片描述

题目解答

模拟

C++

vector<int> decompressRLElist(vector<int>& nums) {
    vector<int> vet;

    int freq, val;
    for(int i = 0; i < nums.size(); i = i+2){
        freq = nums[i];
        val = nums[i + 1];

        for(int j = 0; j < freq; j++){
            vet.emplace_back(val);
        }
    }

    return vet;
}

在这里插入图片描述

java

    public static int[] decompressRLElist(int[] nums) {
        if (nums == null || nums.length == 0 || nums.length % 2 != 0){
            return null;
        }

        int[][] map = new int[nums.length/2][2];

        int a = 0;
        int length = 0;
        for(int i = 1; i < nums.length; i = i + 2){
            int freq = nums[i - 1];
            int value = nums[i];

            map[a][0] = freq;
            map[a][1] = value;
            a++;

            length = length + freq;
        }

        // N

        int[] res = new int[length];
        int start = 0;
        for (int i = 0; i < map.length; i++){
            int freq = map[i][0];
            int value = map[i][1];

            System.out.println(freq + " " + value);

            // 注意这里赋值的时候的起始索引和结束索引
            for (int j = start; j < start +  freq; j++){
                res[j] = value;
            }
            start = start + freq;
        }


        return res;
    }

复杂度N

解答[2ms]

         public static int[] decompressRLElist(int[] nums) {
        if (nums == null || nums.length == 0 || nums.length % 2 != 0){
            return null;
        }

        // 先求出新数组的长度
        int length = 0;
        for(int i = 0; i < nums.length; i = i + 2){
            length = length + nums[i];
        }

        // 填充
        int[] res = new int[length];
        int s = 0;  //
        for (int i = 0; i < nums.length; i = i + 2){
            int a = nums[i]; //
            while (a > 0){
                res[s] = nums[i+1];
                a--;
                s++;
            }
        }


        return res;
    }

solution 【2ms,但是空间复杂度最大】

数组长度说明:因为题目说明了数组长度nums.length最大为100,数据值nums[i]最大100,假设都达到了最大值,则数据规模上限为100个数里有100个100,其中题目所说的a有50个100,说明存储最终结果的数组最大长度为50*100=5000,得初始化容量int[] ans = new int[5000];

解题思路:

直接遍历nums数组,步长为2( i+=2 )
从结果数组ans的填充下标开始位置fromIndex填充到toIndex = from + nums[i]位置
遍历结束,返回数组前toIndex个数

        if (nums == null || nums.length == 0 || nums.length % 2 != 0){
            return null;
        }


        int[] res = new int[50 * 100];

        // sindex 记录为填充的开始索引
        // 开始索引和结束索引的位置
        int toindex = 0;  // 结束索引
        for (int i = 0, fromindex = 0; i < nums.length; i = i + 2){
            toindex = fromindex + nums[i];  // 结束索引 = 开始索引 + 元素长度
            while (fromindex < toindex){
                res[fromindex++] = nums[i+1];
            }
        }

        return Arrays.copyOf(res, toindex);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值