Merge Two Sorted Arrays
Merge two given sorted integer array A and B into a new sorted integer array.
A=[1,2,3,4]
B=[2,4,5,6]
return [1,2,2,3,4,4,5,6]
class Solution {
/**
* @param A and B: sorted integer array A and B.
* @return: A new sorted integer array
* cnblogs.com/beiyeqingteng/
*/
public int[] mergeSortedArray(int[] A, int[] B) {
int[] newArray = new int[A.length + B.length]; int pointer = ; int pointerA = ;
int pointerB = ; while (pointerA < A.length || pointerB < B.length) {
if (pointerB == B.length || pointerA < A.length && A[pointerA] <= B[pointerB]) {
newArray[pointer] = A[pointerA];
pointerA++;
} else {
newArray[pointer] = B[pointerB];
pointerB++;
}
pointer++;
} return newArray;
}
}
转载请注明出处:cnblogs.com/beiyeqingteng/
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 ...
- [Algorithm] 6. Merge Two Sorted Arrays
Description Merge two given sorted integer array A and B into a new sorted integer array. Example A= ...
- 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 ...
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- 【转载】两个排序数组的中位数 / 第K大元素(Median of Two Sorted Arrays)
转自 http://blog.csdn.net/zxzxy1988/article/details/8587244 给定两个已经排序好的数组(可能为空),找到两者所有元素中第k大的元素.另外一种更加具 ...
- LeetCode—— Median of Two Sorted Arrays
Description: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the medi ...
- LeetCode——Merge k Sorted Lists
Discription: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its ...
- LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)
题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...
随机推荐
- java操作word
一个使用Apache POI写word文档的实例: 1 package apache.poi; 2 3 import java.io.ByteArrayInputStream; 4 import ja ...
- python 2.7的安装
最近准备入手学习python 这里我是按照:http://blog.csdn.net/jcjc918/article/details/11022345 来的 我在安装python 3 的时候发现上下左 ...
- bootstrap fileinput添加上传成功回调事件
国外牛人做的bootstrap fileinput挺酷的,但是可惜没有提供自定义上传成功回调事件的接口,因此感到非常头疼,但是很幸运的是,我在网上搜索到一个提问帖子,它问到使用Jquery的on函数绑 ...
- 缓存插件 EHCache
EHCache是来自sourceforge(http://ehcache.sourceforge.net/)的开源项目,也是纯Java实现的简单.快速的Cache组件. 下载jar包 Ehcache ...
- html+Ajax和JSP的比较
1.有人说JSP会泄露源码(可能会有一些代码痕迹,但肯定没啥大事)2.又说,Ajax是为了分离前后台,让控制部分在前台处理,降低代码耦合度,后台只相当于服务. 3.能够让前台移植,降低后期维护成本.纯 ...
- 42.Android之ListView中ArrayAdapter简单学习
今天学习下Android中ListView关于ArrayAdapter数据绑定, 废话少说直接上代码. 改下布局文件: <?xml version="1.0" encodin ...
- POJ2492 A Bug's Life
Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 33833 Accepted: 11078 Description Ba ...
- android studio问题-ICCP:Not recognizing known sRGB profile
转:http://my.oschina.net/1pei/blog/479162 PNG格式:每个PNG文件是由一个PNG标识(signature),后面跟一些数据块(chunk),每个chunk由 ...
- NYOJ298点的转换(矩阵十大问题之一)
点的变换 时间限制:2000 ms | 内存限制:65535 KB 难度:5 描述 平面上有不超过10000个点,坐标都是已知的,现在可能对所有的点做以下几种操作: 平移一定距离(M),相对X ...
- 使用log4net连接Mysql数据库配置
log4net配置: //Author:GaoBingBing [assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net ...