public static void main(String[] args) { int[] nums1 = {1, 3, 41, 56, 78, 89, 100, 299}; int[] nums2 = {0, 2, 30, 50, 80, 99, 111, 180, 999}; int[] total = new int[nums1.length + nums2.length]; //两个数组对比,每次取一个最小的数,取完后排除掉该数(该数组下标+1),直到取完两个数组的所有的数 int indexForOne = 0, indexForTwo = 0; for(int i = 0; i < total.length; i ++) { if(indexForOne == nums1.length) { //如果第一个数组中的数已经取完了,那就直接从第二个取 total[i] = nums2[indexForTwo]; indexForTwo ++; }else if(indexForTwo == nums2.length) { total[i] = nums1[indexForOne]; indexForOne ++; }else if(nums1[indexForOne] <= nums2[indexForTwo]) { total[i] = nums1[indexForOne]; indexForOne ++; }else { total[i] = nums2[indexForTwo]; indexForTwo ++; } System.out.print(total[i] + " "); } }
java合并两个升序数组为一个新的升序数组(不使用排序算法)
猜你喜欢
转载自www.cnblogs.com/hanzx/p/10115794.html
今日推荐
周排行