[LeetCode] 88. Merge Sorted Array
You are given two integer arrays nums1
and nums2
, sorted in non-decreasing order, and two integers m
and n
, representing the number of elements in nums1
and nums2
respectively.
Merge nums1
and nums2
into a single array sorted in non-decreasing order.
The final sorted array should not be returned by the function, but instead be stored inside the array nums1
. To accommodate this, nums1
has a length of m + n
, where the first m
elements denote the elements that should be merged, and the last n
elements are set to 0
and should be ignored. nums2
has a length of n
.
Example 1:
Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3 Output: [1,2,2,3,5,6] Explanation: The arrays we are merging are [1,2,3] and [2,5,6]. The result of the merge is [1,2,2,3,5,6] with the underlined elements coming from nums1.
Example 2:
Input: nums1 = [1], m = 1, nums2 = [], n = 0 Output: [1] Explanation: The arrays we are merging are [1] and []. The result of the merge is [1].
Example 3:
Input: nums1 = [0], m = 0, nums2 = [1], n = 1 Output: [1] Explanation: The arrays we are merging are [] and [1]. The result of the merge is [1]. Note that because m = 0, there are no elements in nums1. The 0 is only there to ensure the merge result can fit in nums1.
Constraints:
nums1.length == m + n
nums2.length == n
0 <= m, n <= 200
1 <= m + n <= 200
-109 <= nums1[i], nums2[j] <= 109
Follow up: Can you come up with an algorithm that runs in O(m + n)
time?
合并两个有序数组。
给你两个按 非递减顺序 排列的整数数组 nums1 和 nums2,另有两个整数 m 和 n ,分别表示 nums1 和 nums2 中的元素数目。
请你 合并 nums2 到 nums1 中,使合并后的数组同样按 非递减顺序 排列。
注意:最终,合并后数组不应由函数返回,而是存储在数组 nums1 中。为了应对这种情况,nums1 的初始长度为 m + n,其中前 m 个元素表示应合并的元素,后 n 个元素为 0 ,应忽略。nums2 的长度为 n 。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/merge-sorted-array
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
既然两个数组都已经排好序,而且nums1的size保证能塞得下两个数组长度之和。思路是从后往前比较谁的元素大,谁就把元素放到nums1里面。这样nums1会被下标从右到左,值从大到小被填满。
时间O(m + n)
空间O(1)
Java实现
1 class Solution { 2 public void merge(int[] nums1, int m, int[] nums2, int n) { 3 m = m - 1; 4 n = n - 1; 5 int i = m + n + 1; 6 while (m >= 0 || n >= 0) { 7 if (m < 0) { 8 nums1[i] = nums2[n]; 9 i--; 10 n--; 11 } else if (n < 0) { 12 nums1[i] = nums1[m]; 13 i--; 14 m--; 15 } else { 16 nums1[i--] = nums1[m] > nums2[n] ? nums1[m--] : nums2[n--]; 17 } 18 } 19 } 20 }
JavaScript实现
1 /** 2 * @param {number[]} nums1 3 * @param {number} m 4 * @param {number[]} nums2 5 * @param {number} n 6 * @return {void} Do not return anything, modify nums1 in-place instead. 7 */ 8 var merge = function(nums1, m, nums2, n) { 9 m = m - 1; 10 n = n - 1; 11 let i = m + n + 1; 12 while (m >= 0 || n >= 0) { 13 if (m < 0) { 14 nums1[i] = nums2[n]; 15 i--; 16 n--; 17 } else if (n < 0) { 18 nums1[i] = nums1[m]; 19 i--; 20 m--; 21 } else { 22 nums1[i--] = nums1[m] > nums2[n] ? nums1[m--] : nums2[n--]; 23 } 24 } 25 };
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类