[LeetCode—001][Java]合并排序的数组

题目描述:
给定两个排序后的数组 A 和 B,其中 A 的末端有足够的缓冲空间容纳B。编写一个方法,将 B 合并入 A 并排序。初始化 A 和 B 的元素数量分别为 m 和 n。

示例:
输入:
A = [1,2,3,0,0,0], m = 3
B = [2,5,6],       n = 3
输出: [1,2,2,3,5,6]

解法一:
直接使用System的快速复制数组的arraycopy(Object src, int srcPos, Object dest, int destPos, int length)方法。

public void merge1(int[] A, int m, int[] B, int n) {
        /*
		src - 源数组。
		srcPos - 源数组中的起始位置。
		dest - 目标数组。
		destPos - 目标数据中的起始位置。
		length - 要复制的数组元素的数量。*/
        System.arraycopy(B,0,A,n,m);
        //使用数组自己的排序方法排序(升序)
        Arrays.sort(A);
        System.out.println(Arrays.toString(A));
    }

测试案例:

public static void main(String[] args) {
		int[] A = {1,2,3,0,0,0};
		int[] B = {2,5,6};
		LeetCode1 lc1 = new LeetCode1();
		lc1.merge1(A, 3, B, 3);
	}

结果:
测试结果
注:

//若直接使用toString方法,得到的是数组的地址
        System.out.println(A.toString());

测试结果
java里,所有的类,不管是java库里面的类,或者是你自己创建的类,全部是从object这个类继承的。object里有一个方法就是toString(),所以所有的类创建的时候,都有一个toString的方法。
  首先我们得了解,java输出用的函数print();是不接受对象直接输出的,只接受字符串或者数字之类的输出。
  当print检测到输出的是一个对象而不是字符或者数字时,那么它会去调用这个对象类里面的toString 方法,输出结果为[类型@哈希值]。Object类中的toString()方法的源代码如下:

 /**
     * Returns a string representation of the object. In general, the
     * {@code toString} method returns a string that
     * "textually represents" this object. The result should
     * be a concise but informative representation that is easy for a
     * person to read.
     * It is recommended that all subclasses override this method.
     * <p>
     * The {@code toString} method for class {@code Object}
     * returns a string consisting of the name of the class of which the
     * object is an instance, the at-sign character `{@code @}', and
     * the unsigned hexadecimal representation of the hash code of the
     * object. In other words, this method returns a string equal to the
     * value of:
     * <blockquote>
     * <pre>
     * getClass().getName() + '@' + Integer.toHexString(hashCode())
     * </pre></blockquote>
     *
     * @return  a string representation of the object.
     */
    public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }

在Arrays类中,直接继承了Object的toString方法,没有重写,只添加了重载的方法:
API1.6里的Arrays类

PS:
System 类包含一些有用的类字段和方法。它不能被实例化。
在 System 类提供的设施中,有标准输入、标准输出和错误输出流;对外部定义的属性和环境变量的访问;加载文件和库的方法;还有快速复制数组的一部分的实用方法。

方法二:

直接for循环带走:

public void merge2(int[] A, int m, int[] B, int n) {
			for(int i = m,j =0;i<m+n;i++,j++) {
				A[i]=B[j];
			}
			Arrays.sort(A);
		System.out.println(Arrays.toString(A));
    }

结果:
测试结果
方法三:
因为两个数组都是有序的,我们可以使用双指针方法。这一方法将两个数组看作队列,每次从两个数组头部取出比较小的数字放到结果中。如下面的动画所示:
在这里插入图片描述
由于Java中没有指针,我们只能模拟一下这种情况:

public void merge3(int[] A, int m, int[] B, int n) {
        // 先确保将其中一个数组中的数字遍历完
        while (m > 0 && n > 0) {
            // 对比选出较大的数放在 m + n - 1 的位置,并将选出此数的指针向前移动
            A[m + n - 1] = A[m - 1] > B[n - 1] ? A[m-- - 1] : B[n-- - 1];
        }
        // 剩下的数都比已经遍历过的数小
        // 如果 m 不为 0,则 A 没遍历完,都已经在 A 中不用再管
        // 如果 n 不为 0,则 B 没遍历完,直接全移到 A 中相同的位置
        while (n > 0) {
            A[n - 1] = B[n - 1];
            n--;
        }
        System.out.println(Arrays.toString(A));
	}

结果:
测试结果

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/sorted-merge-lcci

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值