Microsoft - Union Two Sorted List with Distinct Value

Posted IncredibleThings

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Microsoft - Union Two Sorted List with Distinct Value相关的知识,希望对你有一定的参考价值。

Union Two Sorted List with Distinct Value

 

  • Given X = { 10, 12, 16, 20 } &  Y = {12, 18, 20, 22}
  • We would like to find out the union of two sorted arrays.
  • Union of X U Y is {10, 12, 16, 18, 20, 22}

 

Array:

    public static List<Integer> unionTwoSortedArray(int[] arr1, int[] arr2){
        int len1 = arr1.length;
        int len2 = arr2.length;
        int i = 0;
        int j = 0;
        List<Integer> list = new ArrayList<>();
        while(i<len1 && j<len2){
            if(arr1[i] < arr2[j]){
                list.add(arr1[i]);
                i++;
            }
            else if(arr1[i] > arr2[j]){
                list.add(arr2[j]);
                j++;
            }
            else{
                list.add(arr2[j]);
                i++;
                j++;
            }
        }
        while(i<len1){
            list.add(arr1[i]);
            i++;
        }
        while(j<len2){
            list.add(arr2[j]);
            j++;
        }
        return list;
    }

 

以上是关于Microsoft - Union Two Sorted List with Distinct Value的主要内容,如果未能解决你的问题,请参考以下文章

Union and Intersection of two sorted list

LeetCode-Microsoft-Add Two Numbers II

如何在 React 和 Typescript 中映射我的 Union 类型的 GraphQL 响应数组

spring源码讲解支线任务——AutowiredAnnotationBeanPostProcessor

SOR迭代法实验报告c语言,数学实验“线性方程组的J-迭代,GS-迭代,SOR-迭代解法”实验报告(内含matlab程序代码).doc...

spring中AutowiredAnnotationBeanPostProcessor的注册时机