zoukankan      html  css  js  c++  java
  • java合并两个升序数组为一个新的升序数组(不使用排序算法)

        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] + " ");
            }
        }
  • 相关阅读:
    mysql_单表查询
    mysql_建表
    MySQL基础
    JS_左边栏菜单
    Vue框架之组件系统
    Vue常用语法及命令
    Django的缓存,序列化,ORM操作的性能
    Django中的form表单
    Django中的auth模块
    AJAX请求提交数据
  • 原文地址:https://www.cnblogs.com/hanzx/p/10115794.html
Copyright © 2011-2022 走看看