C/C++教程

1868. Product of Two Run-Length Encoded Arrays

本文主要是介绍1868. Product of Two Run-Length Encoded Arrays,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

My Solution:

class Solution {
    public List<List<Integer>> findRLEArray(int[][] encoded1, int[][] encoded2) {
        int i=0,j=0;
        List<int[]> list = new ArrayList<>();
        while(i<encoded1.length && j<encoded2.length){
            int k1 = encoded1[i][1];
            int k2 = encoded2[j][1];
            int prod = encoded1[i][0]*encoded2[j][0];
            if(k1==k2){
                list.add(new int[]{prod, k1});
                i++;
                j++;
            }else if(k1<k2){
                list.add(new int[]{prod, k1});
                encoded2[j][1] = k2-k1;
                i++;
            }else if(k1>k2){
                list.add(new int[]{prod, k2});
                encoded1[i][1] = k1-k2;
                j++;
            }
        }
        
        List<List<Integer>> res = new ArrayList<>();
        int[] last = list.get(0);int[] cur = null;
        for(int k=1;k<list.size();k++){
            cur = list.get(k);
            if(last[0]==cur[0]){
                cur[1]+=last[1];
                last = cur;
            }
            else{
                res.add(Arrays.asList(new Integer[]{last[0], last[1]}));
                last = cur;
            }
        }
        res.add(Arrays.asList(new Integer[]{cur[0], cur[1]}));
        return res;
    }
}

 

这篇关于1868. Product of Two Run-Length Encoded Arrays的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!