一、问题
给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使 nums1 成为一个有序数组。初始化 nums1 和 nums2 的元素数量分别为 m 和 n 。可以假设 nums1 的空间大小等于 m + n,这样它就有足够的空间保存来自 nums2 的元素。
1️⃣示例 1:输入:nums1 = [1,2,3,0,0,0],m = 3,nums2 = [2,5,6],n = 3
输出:[1,2,2,3,5,6]
2️⃣示例 2:输入:nums1 = [1],m = 1,nums2 = [],n = 0
输出:[1]
提示:
- nums1.length == m + n
- nums2.length == n
- 0 <= m, n <= 200
- 1 <= m + n <= 200
- -10^9 <= nums1[i], nums2[i] <= 10^9
二、解答
1️⃣方法一 : 合并后排序
最朴素的解法就是将两个数组合并之后再排序。时间复杂度较差,为 O((n+m)log(n+m))。这是由于这种方法没有利用两个数组本身已经有序这一点。
public void merge(int[] nums1, int m, int[] nums2, int n) {
System.arraycopy(nums2, 0, nums1, m, n);
Arrays.sort(nums1);
}
①时间复杂度:O((n+m)log(n+m))。
②空间复杂度:O(1)。
2️⃣方法二 : 双指针 / 从前往后
一般,对于有序数组可以通过双指针法达到 O(n+m) 的时间复杂度。最直接的算法实现是将指针 p1 置为 nums1 的开头,p2 为 nums2 的开头,在每一步将最小值放入输出数组中。由于 nums1 是用于输出的数组,需要将 nums1 中的前 m 个元素放在其他地方,也就需要 O(m) 的空间复杂度。public void merge(int[] nums1, int m, int[] nums2, int n) {
// Make a copy of nums1.
int[] nums1_copy = new int[m];
System.arraycopy(nums1, 0, nums1_copy, 0, m);
// Two get pointers for nums1_copy and nums2.
int p1 = 0;
int p2 = 0;
// Set pointer for nums1
int p = 0;
// Compare elements from nums1_copy and nums2
// and add the smallest one into nums1.
while ((p1 < m) && (p2 < n))
nums1[p++] = (nums1_copy[p1] < nums2[p2]) ? nums1_copy[p1++] : nums2[p2++];
// if there are still elements to add
if (p1 < m)
System.arraycopy(nums1_copy, p1, nums1, p1 + p2, m + n - p1 - p2);
if (p2 < n)
System.arraycopy(nums2, p2, nums1, p1 + p2, m + n - p1 - p2);
}
①时间复杂度 : O(n + m)。
②空间复杂度 : O(m)。
3️⃣方法三 : 双指针 / 从后往前
方法二已经取得了最优的时间复杂度 O(n+m),但需要使用额外空间。这是由于在从头改变 nums1 的值时,需要把 nums1 中的元素存放在其他位置。如果从结尾开始改写 nums1 的值又会如何呢?这里没有信息,因此不需要额外空间。这里的指针 p 用于追踪添加元素的位置。
public void merge(int[] nums1, int m, int[] nums2, int n) {
// two get pointers for nums1 and nums2
int p1 = m - 1;
int p2 = n - 1;
// set pointer for nums1
int p = m + n - 1;
// while there are still elements to compare
while ((p1 >= 0) && (p2 >= 0))
// compare two elements from nums1 and nums2
// and add the largest one in nums1
nums1[p--] = (nums1[p1] < nums2[p2]) ? nums2[p2--] : nums1[p1--];
// add missing elements from nums2
System.arraycopy(nums2, 0, nums1, 0, p2 + 1);
}
①时间复杂度 : O(n + m)。
②空间复杂度 : O(1)。