[LeetCode] 88. Merge Sorted Array 混合插入有序数组
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
Note:
- The number of elements initialized in nums1and nums2 are m and n respectively.
- You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2.
Example:
Input:
nums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6], n = 3 Output: [1,2,2,3,5,6]
混合插入有序数组,由于两个数组都是有序的,所有只要按顺序比较大小即可。题目中说了 nums1 数组有足够大的空间,说明不用 resize 数组,又给了m和n,那就知道了混合之后的数组的大小,这样就从 nums1 和 nums2 数组的末尾开始一个一个比较,把较大的数,按顺序从后往前加入混合之后的数组末尾。需要三个变量 i,j,k,分别指向 nums1,nums2,和混合数组的末尾。进行 while 循环,如果i和j都大于0,再看如果 nums1[i] > nums2[j],说明要先把 nums1[i] 加入混合数组的末尾,加入后k和i都要自减1;反之就把 nums2[j] 加入混合数组的末尾,加入后k和j都要自减1。循环结束后,有可能i或者j还大于等于0,若j大于0,那么还需要继续循环,将 nums2 中的数字继续拷入 nums1。若是i大于等于0,那么就不用管,因为混合数组本身就放在 nums1 中,参见代码如下:
解法一:
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int i = m - , j = n - , k = m + n - ;
while (i >= && j >= ) {
if (nums1[i] > nums2[j]) nums1[k--] = nums1[i--];
else nums1[k--] = nums2[j--];
}
while (j >= ) nums1[k--] = nums2[j--];
}
};
我们还可以写的更简洁一些,将两个 while 循环融合到一起,只要加上 i>=0 且 nums1[i] > nums2[j] 的判断条件,就可以从 nums1 中取数,否则就一直从 nums2 中取数,参见代码如下:
解法二:
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int i = m - , j = n - , k = m + n - ;
while (j >= ) {
nums1[k--] = (i >= && nums1[i] > nums2[j]) ? nums1[i--] : nums2[j--];
}
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/88
类似题目:
Squares of a Sorted Array
Interval List Intersections
参考资料:
https://leetcode.com/problems/merge-sorted-array/
https://leetcode.com/problems/merge-sorted-array/discuss/29572/My-simple-solution
https://leetcode.com/problems/merge-sorted-array/discuss/29515/4ms-C%2B%2B-solution-with-single-loop
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 88. Merge Sorted Array 混合插入有序数组的更多相关文章
- [LeetCode] Merge Sorted Array 混合插入有序数组
Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...
- LeetCode 88. Merge Sorted Array(合并有序数组)
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...
- leetCode 88.Merge Sorted Array (合并排序数组) 解题思路和方法
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: Y ...
- Leetcode#88. Merge Sorted Array(合并两个有序数组)
题目描述 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m ...
- [LeetCode] 88. Merge Sorted Array 合并有序数组
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...
- [leetcode]88. Merge Sorted Array归并有序数组
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...
- LeetCode 88 Merge Sorted Array
Problem: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array ...
- Leetcode 88. Merge Sorted Array(easy)
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...
- Leetcode 88 Merge Sorted Array STL
合并有序数组 时间复杂度O(m+n) 该算法来自各种算法与数据结构书,写得已经烂得不能再烂了,这个应该是最短的代码了吧,不知如何归类 class Solution { public: void mer ...
随机推荐
- MySQL 合并字段及列转行
数据表: 列转行:利用max(case when then) max---聚合函数 取最大值 (case course when '语文' then score else 0 end) ---判断 ...
- vuex 源码分析(七) module和namespaced 详解
当项目非常大时,如果所有的状态都集中放到一个对象中,store 对象就有可能变得相当臃肿. 为了解决这个问题,Vuex允许我们将 store 分割成模块(module).每个模块拥有自己的 state ...
- Vue使用小结
公司新项目使用Asp.Net Core+Vue组合来做,这里总结下对于Vue的认识 为什么选择Vue 主要基于以下几点选择Vue而不是jQuery.React等框架 双向绑定相比于jQuery减少了许 ...
- .NET Core 2.1 以下的控制台应用程序生成 EXE,且使用命令行参数动态运行控制器应用程序的示例
文章: https://stackoverflow.com/questions/44038847/vs2017-compile-netcoreapp-as-exe 引用 <ItemGroup&g ...
- (转)dnSpy 强大的.Net反编译软件
目录 1. Debug外部引用的Dll文件2. 调试应用程序3. 修改exe文件的内容 作者:D.泡沫 一说起.net的反编译软件,大家首先想到的就是Reflector,ILSpy,dotPeek等等 ...
- MVC三层架构搭建
MVC三层架构搭建 项目主要是用三层来搭建项目,三层分为表现层,数据层和业务层.项目用了目前比较流行的IOC架构.目前流行的IoC 框架有AutoFac,Unity,Spring.NET等,项目中选用 ...
- Java获取客户端真实IP地址
Java代码 import javax.servlet.http.HttpServletRequest; /** * 获取对象的IP地址等信息 */ public class IPUtil { /** ...
- CTF必备技能丨Linux Pwn入门教程——ROP技术(上)
Linux Pwn入门教程系列分享如约而至,本套课程是作者依据i春秋Pwn入门课程中的技术分类,并结合近几年赛事中出现的题目和文章整理出一份相对完整的Linux Pwn教程. 教程仅针对i386/am ...
- swift(二)swift字符串和字符和逻辑运算
/* 1.swift字符串和字符 2.构造字符串 3.字符串比较 4.数值运算 5.复制运算 6.关系运算 7.逻辑运算 8.区间运算 */ /* //数据 + 数据的处理 //字符信息+ 字符信息的 ...
- 【原】通过Spring结合Cglib处理非接口代理
前言: 之前做的一个项目,虽然是查询ES,但内部有大量的逻辑计算,非常耗时,而且经常收到JVM峰值告警邮件.分析了一下基础数据每天凌晨更新一次,但查询和计算其实在第一次之后就可以写入缓存,这样后面直接 ...