88. 合并两个有序数组
题目来源:88. 合并两个有序数组
给你两个有序整数数组 nums1 和 nums2,请你将 nums2 合并到 nums1 中,使 nums1 成为一个有序数组。
初始化 nums1 和 nums2 的元素数量分别为 m 和 n 。你可以假设 nums1 的空间大小等于 m + n,
这样它就有足够的空间保存来自 nums2 的元素。
方法一:直接合并后排序
var merge = function(nums1, m, nums2, n) { nums1.splice(m, n, ...nums2) return nums1.sort((a, b)=>a-b); };
python3
class Solution:
def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
"""
Do not return anything, modify nums1 in-place instead.
"""
nums1[m:] = nums2
nums1.sort()
方法二:双指针
/** * @param {number[]} nums1 * @param {number} m * @param {number[]} nums2 * @param {number} n * @return {void} Do not return anything, modify nums1 in-place instead. */ var merge = function(nums1, m, nums2, n) { let newArr = new Array(m+n).fill(0); let s1 = 0, s2 = 0; let cur = 0; while(s1 < m || s2 < n){ if(s1 == m){ cur = nums2[s2++]; }else if(s2 == n){ cur = nums1[s1++]; }else if(nums1[s1] > nums2[s2]){ cur = nums2[s2++]; }else{ cur = nums1[s1++]; } newArr[s1 + s2 - 1] = cur; } let i = 0; for(let num of newArr){ nums1[i++] = num; } return nums1; };
from typing import List class Solution: def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None: """ Do not return anything, modify nums1 in-place instead. """ tmp = [] p1, p2 = 0, 0 while p1<m or p2<n: if(p1 == m): tmp.append(nums2[p2]) p2 += 1 elif(p2 == n): tmp.append(nums1[p1]) p1 += 1 elif(nums1[p1] < nums2[p2]): tmp.append(nums1[p1]) p1 += 1 else: tmp.append(nums2[p2]) p2 += 1 nums1[:] = tmp so = Solution() nums1 = [1,2,3,0,0,0] m = 3 nums2 = [2,5,6] n = 3 so.merge(nums1, m, nums2, n) print(nums1 ) nums1 = [1] m = 1 nums2 = [] n = 0 so.merge(nums1, m, nums2, n) print(nums1 )
方法三:逆向双指针
/** * @param {number[]} nums1 * @param {number} m * @param {number[]} nums2 * @param {number} n * @return {void} Do not return anything, modify nums1 in-place instead. */ var merge = function(nums1, m, nums2, n) { let m1 = m - 1; let n2 = n-1; let len = m+n-1; while(m1>=0&&n2>=0){ nums1[len--] = (nums1[m1] > nums2[n2]) ? nums1[m1--]:nums2[n2--]; } while(n2>=0){ nums1[len--] = nums2[n2--] } return nums1; } let nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3 console.log(nums1, nums2,m,n ,merge(nums1,m,nums2,n)) nums1 = [1], m = 1, nums2 = [], n = 0 console.log(nums1, nums2,m,n ,merge(nums1,m,nums2,n))
from typing import List class Solution: def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None: """ Do not return anything, modify nums1 in-place instead. """ p1, p2 = m-1, n-1 tail = m + n -1 while p1 >=0 or p2 >=0: if(p1 == -1): nums1[tail] = nums2[p2] p2 -= 1 elif(p2 == -1): nums1[tail] = nums1[p1] p1 -= 1 elif(nums1[p1] > nums2[p2]): nums1[tail] = nums1[p1] p1 -= 1 else: nums1[tail] = nums2[p2] p2 -= 1 tail -= 1 so = Solution() nums1 = [1,2,3,0,0,0] m = 3 nums2 = [2,5,6] n = 3 so.merge(nums1, m, nums2, n) print(nums1 ) nums1 = [1] m = 1 nums2 = [] n = 0 so.merge(nums1, m, nums2, n) print(nums1 )
示例 1:
输入:nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
输出:[1,2,2,3,5,6]
示例 2:
输入:nums1 = [1], m = 1, nums2 = [], n = 0
输出:[1]
提示:
nums1.length == m + n
nums2.length == n
0 <= m, n <= 200
1 <= m + n <= 200
-109 <= nums1[i], nums2[i] <= 109