[Algorithm] 6. Merge Two Sorted Arrays
Description
Merge two given sorted integer array A and B into a new sorted integer array.
Example
A=[1,2,3,4]
B=[2,4,5,6]
return [1,2,2,3,4,4,5,6]
Challenge
How can you optimize your algorithm if one array is very large and the other is very small?
Solution
/**
* @param A: sorted integer array A
* @param B: sorted integer array B
* @return: A new sorted integer array
*/
vector<int> mergeSortedArray(vector<int> &A, vector<int> &B) {
// write your code here
vector<int> res;
int maxSize= A.size() > B.size() ? A.size() : B.size();
int index_a=,index_b=; for(int i=; i<A.size()+B.size(); i++)
{
if(index_b==B.size()){
for(int i=index_a; i<A.size(); i++) res.push_back(A[i]);
return res;
} if(index_a==A.size()){
for(int i=index_b; i<B.size(); i++) res.push_back(B[i]);
return res;
} if (A[index_a] <= B[index_b]){
res.push_back(A[index_a]);
index_a++;
}else{
res.push_back(B[index_b]);
index_b++;
}
}
return res;
}
Tips
Select the minimum element from two arrays in every loop.
[Algorithm] 6. Merge Two Sorted Arrays的更多相关文章
- Merge k Sorted Arrays【合并k个有序数组】【优先队列】
Given k sorted integer arrays, merge them into one sorted array. Example Given 3 sorted arrays: [ [1 ...
- 怎样合并排序数组(How to merge 2 sorted arrays?)
Question: We have 2 sorted arrays and we want to combine them into a single sorted array. Input: arr ...
- LeetCode Algorithm 04_Median of Two Sorted Arrays
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...
- [Algorithm] 21. Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
- Merge Two Sorted Arrays
Merge two given sorted integer array A and B into a new sorted integer array. Example A=[1,2,3,4] B= ...
- Merge K Sorted Arrays
This problem can be solved by using a heap. The time is O(nlog(n)). Given m arrays, the minimum elem ...
- algorithm@ find kth smallest element in two sorted arrays (O(log n time)
The trivial way, O(m + n): Merge both arrays and the k-th smallest element could be accessed directl ...
- No.004:Median of Two Sorted Arrays
问题: There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the ...
- [LeetCode] Median of Two Sorted Arrays 两个有序数组的中位数
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
随机推荐
- 01排序的Java实现
比赛描写叙述: 的个数同样时再按ASCII码值排序. 输入: 个字符. 输出: 串的顺序.使得串按基本描写叙述的方式排序. 例子输入: 例子输出: 被AC的代码例如以下: import java.ut ...
- 关于spring配置文件中编辑时没有提示信息的问题
spring配置文件头部信息主要是提供一个xml的编写规范作用. 新创建的配置文件引入头部信息后,编辑时没有提示信息,重启elipse即可解决.
- go4--break,continue + 标签
package main /* 指针 Go虽然保留了指针,但与其它编程语言不同的是,在Go当中不 支持指针运算以及”->”运算符,而直接采用”.”选择符来操作指针 目标对象的成员 操作符”&am ...
- go语言---defer
go语言---defer https://blog.csdn.net/cyk2396/article/details/78885135 defer 是在函数退出前调用,多个defer遵循 先进后出 的 ...
- 用C#读取txt文件的方法(转)
.使用FileStream读写文件 文件头: using System; using System.Collections.Generic; using System.Text; using Syst ...
- P3174 [HAOI2009]毛毛虫(树形dp)
P3174 [HAOI2009]毛毛虫 题目描述 对于一棵树,我们可以将某条链和与该链相连的边抽出来,看上去就象成一个毛毛虫,点数越多,毛毛虫就越大.例如下图左边的树(图 1 )抽出一部分就变成了右边 ...
- bzoj1085骑士精神(搜索)
1085: [SCOI2005]骑士精神 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1893 Solved: 1051 Description ...
- CSS3常用知识点
CSS3常用知识点 1 css3选择器 1.1 属性选择器 /* E[attr~=val] 表示的一个单独的属性值 这个属性值是以空格分隔的*/ .attr2 a[class~="kawa& ...
- 洛谷 P1233 木棍加工
题目描述 一堆木头棍子共有n根,每根棍子的长度和宽度都是已知的.棍子可以被一台机器一个接一个地加工.机器处理一根棍子之前需要准备时间.准备时间是这样定义的: 第一根棍子的准备时间为1分钟: 如果刚处理 ...
- less新手入门(一) 变量、extend扩展
前景提要 个人在学习less时候的学习笔记及个人总结,主要是结合less中文网来学习的,但是说是中文网并不是中文呀,看起来很耽误时间,为了避免以后再次看的时候还要翻译思考,特意做此总结,方便以后查阅. ...