Leetcode 88:合并两个有序数组

题目描述:

给你两个按 非递减顺序 排列的整数数组 nums1 和 nums2,另有两个整数 m 和 n ,分别表示 nums1 和 nums2 中的元素数目。

请你 合并 nums2 到 nums1 中,使合并后的数组同样按 非递减顺序 排列。

注意:最终,合并后数组不应由函数返回,而是存储在数组 nums1 中。为了应对这种情况,nums1 的初始长度为 m + n,其中前 m 个元素表示应合并的元素,后 n 个元素为 0 ,应忽略。nums2 的长度为 n 。

示例 1:

输入:nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3

输出:[1,2,2,3,5,6]

解释:需要合并 [1,2,3] 和 [2,5,6] 。合并结果是 [1,2,2,3,5,6] ,其中斜体加粗标注的为 nums1 中的元素。

示例 2:

输入:nums1 = [1], m = 1, nums2 = [], n = 0

输出:[1]

解释:需要合并 [1] 和 [] 。合并结果是 [1] 。

示例 3:

输入:nums1 = [0], m = 0, nums2 = [1], n = 1

输出:[1]

解释:需要合并的数组是 [] 和 [1] 。合并结果是 [1] 。

注意,因为 m = 0 ,所以 nums1 中没有元素。nums1 中仅存的 0 仅仅是为了确保合并结果可以顺利存放到 nums1 中。

 解法一:拷贝数组,排序

思路:

将两个数组合并成一个数组,然后排序。

代码如下:

class Solution {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        //拷贝数组2到数组1中
        System.arraycopy(nums2, 0, nums1, m, n);
        //排序
        Arrays.sort(nums1);
    }
}

另一种写法:

class Solution {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        //遍历拷贝nums2中的元素到num1中
        int index = 0;
        for(int i=m; i < nums1.length; i++){
            nums1[i] = nums2[index++];
        }
        //对nums1进行排序
        Arrays.sort(nums1);
    }
}

解法二:双指针

思路:

创建一个临时数组,通过双指针分别指向两个数组对元素大小进行判断,按照升序的方式将元素加入临时数组,最后再拷贝给nums1即可。

代码如下:

class Solution {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        //创建一个临时数组
       int[] ret = new int[m+n];
       //依次分别代表nums1、nums2、ret的下标
        int index1 = 0, index2 = 0,index = 0;
        //双指针向后遍历,依次比较大小
        while(index1 < m && index2 < n){
            ret[index++] = nums1[index1] < nums2[index2] ? nums1[index1++] : nums2[index2++];
        }
        //如果nums2遍历完,但是nums1未遍历完,则将nums1剩余元素拷贝到ret中
        if(index1 < m){
            System.arraycopy(nums1, index1, ret, index1+index2, m+n-index1-index2);
        }
        //如果nums1遍历完,但是nums2未遍历完,则将nums2剩余元素拷贝到ret中
        if(index2 < n){
            System.arraycopy(nums2, index2, ret, index1+index2, m+n-index1-index2);
        }
        //将ret中的结果拷贝到nums1中
        System.arraycopy(ret, 0, nums1, 0, ret.length);
    }
}

解法三:逆向双指针

思路:

解法二固然解决了问题,但是里面依旧占用了空间。观察nums1,发现其后半部分仅仅是起到了占位符的作用,因此可以通过逆向双指针的方式可以不使用额外的空间即可完成合并。设置指针index1 和index2分别指向 nums1 和 nums2 的最后面,从后面的元素值开始比较遍历,同时设置指针index指向 nums1 的最末尾,每次遍历比较值大小之后,则进行填充。当index1<0 时遍历结束,此时 nums2 中数据未拷贝完全,将其直接拷贝到 nums1 的前面,最后得到结果数组.

代码如下:

class Solution {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
                //从后向前遍历
        int index1 = m-1, index2 = n-1,index = m+n-1;
        //比较大小
        while(index1 >= 0 && index2 >= 0){
            nums1[index--] = nums1[index1] < nums2[index2] ? nums2[index2--] :nums1[index1--];
        }
        //拷贝剩余元素
        System.arraycopy(nums2, 0, nums1, 0, index2 +1);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值