[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 ...
随机推荐
- ios19---xib
// // ViewController.m #import "ViewController.h" @interface ViewController () @end @imple ...
- Silverlight调用WCF(1)
[置顶] Silverlight调用WCF(1) 分类: 技术2012-03-31 12:29 940人阅读 评论(0) 收藏 举报 wcfsilverlightexceptionusersecuri ...
- java异常——重新抛出异常
有时候希望把刚捕获的异常重新抛出,尤其是在使用Exception捕获所有异常的时候.既然已经得到了对当前异常对象的引用,可以直接把它重新抛出: catch(Exception e){ System.o ...
- iOS:界面适配(二)--iPhone/iPad适配(关于xib)
本文纯属个人看法,强迫症后遗症 版本:xcode 6.0 + iOS SDK 8.0 讨论范围:控制器的view(创建VC时自带的xib) ------------------------------ ...
- 用React & Webpack构建前端新闻网页
这是一篇给初学者的教程, 在这篇教程中我们将通过构建一个 Hacker News 的前端页面来学习 React 与 Webpack. 它不会覆盖所有的技术细节, 因此它不会使一个初学者变成大师, 但希 ...
- 昆石VOS2009 VOS3000无漏洞去后门电脑管理客户端大全
注意:您的VOS是什么版本的请使用什么版本的管理客户端,否则无法登陆! ** VOS2009的: ** VOS2009-client-v2.1.2.0.exe 点击这里下载: VOS2009-clie ...
- js DOM操作练习
1.有如下html,如果用js获得被选中的option的text描述(非value)<select id="select_id"> <option vlue ...
- [Swift通天遁地]一、超级工具-(6)通过JavaScript(脚本)代码调用设备的源生程序
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- HTML <input>标签属性
- 51nod1298 圆与三角形
1298 圆与三角形 题目来源: HackerRank 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出圆的圆心和半径,以及三角形的三个顶点,问圆同三 ...