[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 ...
随机推荐
- Windows10、ARM开发板、VMware虚拟机同时连接Internet
前段时间有人遇到一些网络连接问题,让我帮忙处理,他想让ARM开发板连接外网,可以连接网络数据库,同时保证自己的电脑可以上网. 本来说直接可以连接一个路由器,分配一个内网IP给ARM就可以了,但是当时那 ...
- ‘CONFIG_ENV_SIZE’未声明(不在函数内【转】
本文转载自: http://bbs.csdn.net/topics/390678466 见论坛讨论.可以临时修复.
- POJ 1477:Box of Bricks
Box of Bricks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19949 Accepted: 8029 De ...
- poj 2762(tarjan缩点+判断是否是单链)
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19234 ...
- BZOJ_3790_神奇项链_manacher+贪心
BZOJ_3790_神奇项链_manacher+贪心 Description 母亲节就要到了,小 H 准备送给她一个特殊的项链.这个项链可以看作一个用小写字 母组成的字符串,每个小写字母表示一种颜色. ...
- BZOJ_1692_[Usaco2007 Dec]队列变换_后缀数组
BZOJ_1692_[Usaco2007 Dec]队列变换_后缀数组 Description FJ打算带他的N(1 <= N <= 30,000)头奶牛去参加一年一度的“全美农场主大奖赛” ...
- 【USACO 2017Feb】 Why Did the Cow Cross the Road
[题目链接] 点击打开链接 [算法] dist[i][j][k]表示当前走到(i,j),走的步数除以3的余数为k的最小花费 spfa即可 [代码] #include<bits/stdc++.h& ...
- Java 泛型 四 基本用法与类型擦除
简介 Java 在 1.5 引入了泛型机制,泛型本质是参数化类型,也就是说变量的类型是一个参数,在使用时再指定为具体类型.泛型可以用于类.接口.方法,通过使用泛型可以使代码更简单.安全.然而 Java ...
- bzoj3561
3561: DZY Loves Math VI Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 240 Solved: 163[Submit][Sta ...
- 解决 IDEA 中文乱码
一.打开Intellij的根目录,找到下图的两个文件(根据你的系统是32位或64位选择其中一个配置文件),在配置文件中添加:-Dfile.encoding=UTF-8 二. 配置IDE编码 点击F ...