侧边栏壁纸
博主头像
流殃博主等级

用微笑面对生活

  • 累计撰写 176 篇文章
  • 累计创建 43 个标签
  • 累计收到 2 条评论

目 录CONTENT

文章目录

两个有序数组合并为有序数组

流殃
2021-09-24 / 0 评论 / 0 点赞 / 140 阅读 / 1,123 字 / 正在检测是否收录...

可以重复

    public static void mergeArray(int[] a,int[] b) {

        int length1 = a.length;
        int length2 = b.length;
        int newArrayLength = length1 + length2;
        int[] result = new int[newArrayLength];
        int i = 0, j = 0, k = 0;   //i:用于标示a数组    j:用来标示b数组  k:用来标示传入的数组

        while (i < length1 && j < length2) {
            /* 元素不管重复与否,直接给合并到一起 */
            if (a[i] <= b[j]) {
                result[k++] = a[i++];
            } else {
                result[k++] = b[j++];
            }
        }

        /* 后面while循环是用来保证两个数组比较完之后剩下的一个数组里的元素能顺利传入结果数组 */
        while (i < a.length) {
            result[k++] = a[i++];
        }

        while (j < b.length) {
            result[k++] = b[j++];
        }
        System.out.println(Arrays.toString(result));
}

去出重复

    public static void mergeArray(int[] a,int[] b) {

        int length1 = a.length;
        int length2 = b.length;
        int newArrayLength = length1 + length2;
        int[] result = new int[newArrayLength];
        int i = 0, j = 0, k = 0;   //i:用于标示a数组    j:用来标示b数组  k:用来标示传入的数组

        while (i < length1 && j < length2) {
            /* 去重复元素,但是空间利用率还是浪费啦,看结果后面有默认的2个0显示 */
            if (a[i] < b[j]) {
               result[k++] = a[i++];
           } else if (a[i] == b[j]) {
                result[k++] = a[i];
               //在某个位置上2个值相等的话,取哪个都一样,
              // 然后这个相等的位置的2个值都可以不用比啦,都直接向后移动1,继续比较
               j++;
               i++;
          } else {
              result[k++] = b[j++];
          }
        }

        /* 后面while循环是用来保证两个数组比较完之后剩下的一个数组里的元素能顺利传入结果数组 */
        while (i < a.length) {
            result[k++] = a[i++];
        }

        while (j < b.length) {
            result[k++] = b[j++];
        }
        System.out.println(Arrays.toString(result));
}
0

评论区