[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 ...
随机推荐
- 20170623_oracle_优化与体系结构
一般优化技巧 建议不用"*"代替所有列名 删除所有数据用TRUNCATE代替DELETE 用NOT EXISTS 代替NOT IN 用EXISTS代替IN 用EXISTS代替DIS ...
- C语言8大经典排序算法(1)
算法一直是编程的基础,而排序算法是学习算法的开始,排序也是数据处理的重要内容.所谓排序是指将一个无序列整理成按非递减顺序排列的有序序列.排列的方法有很多,根据待排序序列的规模以及对数据的处理的要求,可 ...
- mybatis写当天 当月的数据 时间段数据
1 数据库字段pk_time(Varchar) 当天的数据 SELECT * FROM 表 WHERE date(fk_time) = curdate(); 当月的数据 SELECT *FROM 表 ...
- Spark 分布式环境---slave节点无法启动(已解决)
soyo@soyo-VPCCB3S1C:~$ start-slaves.sh soyo-slave01: starting org.apache.spark.deploy.worker.Worker, ...
- java静态方法和实例化方法的区别(copy)
[资料来源] http://blog.csdn.net/biaobiaoqi/article/details/6732117 方法是我们每天都在写得,很多程序员大多都使用实例化方法,而很少使用静态方法 ...
- javascript实现引用数据类型的深拷贝和浅拷贝详解
关于引用类型值的详解,请看另一篇随笔 https://www.cnblogs.com/jinbang/p/10346584.html 深拷贝和浅拷贝,也就是引用数据类型栈和堆的知识点.深浅拷贝的原型都 ...
- 洛谷2019 3月月赛 T1
题干 2019第一次月赛 我只有255pts T1还是比较水的... 海星 T1一道简单的模拟(就是有坑..导致很多人不能一次性AC 比如说我) _3个坑点 1.位数问题 2.-0 3.0... #i ...
- jQuery——修改网页字体大小
HTML: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <lin ...
- Android 性能优化(8)网络优化( 4)Optimizing App-Initiated Network Use
Optimizing App-Initiated Network Use This lesson teaches you to Batch and Schedule Network Requests ...
- Hbase源码分析:server端RPC
server端rpc包括master和RegionServer.接下来主要梳理一下,master和regionserver中有关rpc创建,启动以及处理的过程. 1,server rpc的初始化过程 ...