码迷,mamicode.com
首页 > 其他好文 > 详细

Microsoft - Union Two Sorted List with Distinct Value

时间:2018-04-29 10:12:47      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:ati   code   bsp   value   The   ike   stat   with   array   

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

标签:ati   code   bsp   value   The   ike   stat   with   array   

原文地址:https://www.cnblogs.com/incrediblechangshuo/p/8970166.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!