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的更多相关文章

  1. Merge k Sorted Arrays【合并k个有序数组】【优先队列】

    Given k sorted integer arrays, merge them into one sorted array. Example Given 3 sorted arrays: [ [1 ...

  2. 怎样合并排序数组(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 ...

  3. 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 ...

  4. [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 ...

  5. 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= ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. SQL Server: Difference between PARTITION BY and GROUP BY

    https://stackoverflow.com/questions/2404565/sql-server-difference-between-partition-by-and-group-by ...

  2. 假如Java对象是个人······

    假如Java对象是个人,那意味着它也具备了我们人所有的东西,头,身体,大长腿. 头 头就是我们的对象头(Header).根据JAVA虚拟机规范,我们的对象头分为两部分,分别是存储对象自身的运行时数据和 ...

  3. 4.4 Top-Down Parsing

    4.4 Top-Down Parsing Top-down parsing can be viewed as the problem of constructing a parse tree for ...

  4. nodejs实现验证码

    http://www.9958.pw/post/nodejs_lesson http://www.9958.pw/post/nodejscapp

  5. bzoj 1034: [ZJOI2008]泡泡堂BNB【贪心】

    是贪心 先把两个数组排序,然后贪心的选让a数组占优的(如果没有就算输),这是最大值,最小值是2n-贪心选b数组占优 #include<iostream> #include<cstdi ...

  6. TestNG设置用例循环执行

    曾经做过一需求,需要单个集成测试用例循环执行N次,或许你会说for循环就可以了,这当然是可以的.那有没有逼格更高点的方法,当然也是有的.下面我们就说下使用TestNG注解功能实现用例的循环执行. 1. ...

  7. SQL Server触发器创建、删除、修改、查看示例步骤

    一﹕ 触发器是一种特殊的存储过程﹐它不能被显式地调用﹐而是在往表中插入记录﹑更新记录或者删除记录时被自动地激活.所以触发器可以用来实现对表实施复杂的完整性约`束. 二﹕ SQL Server为每个触发 ...

  8. 【洛谷3345_BZOJ3924】[ZJOI2015]幻想乡战略游戏(点分树)

    大概有整整一个月没更博客了 -- 4 月为省选爆肝了一个月,最后压线进 B 队,也算给 NOIP2018 翻车到 316 分压线省一这个折磨了五个月的 debuff 画上了一个不算太差的句号.结果省选 ...

  9. Android 性能优化(26)*性能工具之「Batterystats,Battery Historian」Batterystats & Battery Historian Walkthrough

    Batterystats & Battery Historian Walkthrough Working with Batterystats & Battery Historian B ...

  10. 使用JS分页 <span> beta 3.0 完成封装的分页

    <html> <head> <title>分页</title> <style> #titleDiv{ width:500px; backgr ...