栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

给定已经按降序排列的arr1和arr2,输出一个数组,该数组以降序附加来自arr1和arr2的值

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

给定已经按降序排列的arr1和arr2,输出一个数组,该数组以降序附加来自arr1和arr2的值

尝试这种方式:

public static int[] join(int[] arr1,int[] arr2){        int[] joinArr=new int[arr1.length + arr2.length];        int i=0,j=0,k=0;        while(i<arr1.length && j<arr2.length){  // coping from both the array while one of them is exhausted if( arr1[i]>arr2[j]){     joinArr[k++]=arr1[i++]; // coping from arr1 and update the index i and k. }else if(arr1[i]<arr2[j]){     joinArr[k++]=arr2[j++]; // coping from arr2 and update the index j and k. }else{     joinArr[k++]=arr2[j++]; // coping from any of arr1  or arr2 and update the index i,j and k.      i++; }        }          if(i<arr1.length){  // coping from  the array arr1 since arr2 is exhausted  while(i<arr1.length ){      joinArr[k++]=arr1[i++];  }        }        if(j<arr2.length){  // coping from  the array arr2 since arr1 is exhausted  while(j<arr2.length ){      joinArr[k++]=arr2[j++];  }        }        return Arrays.copyOf(joinArr, k);    }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/418611.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号