6. 合并排序数组 II
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?
public class Solution {
/**
* @param A: sorted integer array A
* @param B: sorted integer array B
* @return: A new sorted integer array
*/
public int[] mergeSortedArray(int[] A, int[] B) {
int i = 0;
int j = 0;
int k = 0;
int[] result = new int[A.length + B.length];
while(i < A.length && j < B.length){
if(A[i] <= B[j]){
result[k++] = A[i];
i++;
}else{
result[k++] = B[j];
j++;
}
}
if(i == A.length){
for(int l = j; l < B.length; l++){
result[k++] = B[l];
}
}else{
for(int m = i ; m < A.length; m++){
result[k++] = A[m];
}
}
return result;
}
}
class Solution {
/**
* @param A: sorted integer array A
* @param B: sorted integer array B
* @return: A new sorted integer array
*/
public int[] mergeSortedArray(int[] A, int[] B) {
// write your code here
if (A.length == 0 || A == null){
return B;
}
if (B.length == 0 || B == null){
return A;
}
int len = A.length + B.length;
int[] res = new int[len];
if(A.length >= B.length) {
for(int i=0;i<A.length;i++) {
res[i] = A[i];
}
for(int i=0;i<B.length;i++) {
res[A.length + i] = B[i];
}
}else {
for(int i=0;i<B.length;i++) {
res[i] = B[i];
}
for(int i=0;i<A.length;i++) {
res[B.length + i] = A[i];
}
}
Arrays.sort(res);
return res;
}
}
描述
合并两个排序的整数数组A和B变成一个新的数组。
您在真实的面试中是否遇到过这个题?
样例
给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6]
6. 合并排序数组 II的更多相关文章
- lintcode:合并排序数组 II
题目: 合并排序数组 II 合并两个排序的整数数组A和B变成一个新的数组. 样例 给出A = [1, 2, 3, empty, empty] B = [4,5] 合并之后A将变成[1,2,3,4,5] ...
- LintCode之合并排序数组II
题目描述: 分析:题目的意思是把数组A和数组B合并到数组A中,且数组A有足够的空间容纳A和B的元素,合并后的数组依然是有序的. 我的代码: public class Solution { /* * @ ...
- LintCode——合并排序数组II
描述:合并两个排序的整数数组A和B变成一个新的数组 样例:给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6] 1.Python:先将数组B加到数组A之后,然后 ...
- [LintCode] 合并排序数组II
class Solution { public: /** * @param A: sorted integer array A which has m elements, * but size of ...
- [容易]合并排序数组 II
题目来源:http://www.lintcode.com/zh-cn/problem/merge-sorted-array/
- lintcode-64-合并排序数组 II
64-合并排序数组 II 合并两个排序的整数数组A和B变成一个新的数组. 注意事项 你可以假设A具有足够的空间(A数组的大小大于或等于m+n)去添加B中的元素. 样例 给出 A = [1, 2, 3, ...
- lintcode 中等题:搜索旋转排序数组II
题目 搜索旋转排序数组 II 跟进“搜索旋转排序数组”,假如有重复元素又将如何? 是否会影响运行时间复杂度? 如何影响? 为何会影响? 写出一个函数判断给定的目标值是否出现在数组中. 样例 给出[3, ...
- lintcode:合并排序数组
题目: 合并排序数组 合并两个排序的整数数组A和B变成一个新的数组. 样例 给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6] 挑战 你能否优化你的算法,如果 ...
- lintcode-63-搜索旋转排序数组 II
63-搜索旋转排序数组 II 跟进"搜索旋转排序数组",假如有重复元素又将如何? 是否会影响运行时间复杂度? 如何影响? 为何会影响? 写出一个函数判断给定的目标值是否出现在数组中 ...
随机推荐
- 拓扑排序基础 hdu1258,hdu2647
由这两题可知拓扑排序是通过“小于”关系加边建图的 hdu2647 /* 拓扑排序的原则是把“小于”看成有向边 此题反向建图即可 并且开num数组来记录每个点的应该得到的权值 */ #include&l ...
- 动手动脑——JAVA语法基础
EnumTest.java public class EnumTest { public static void main(String[] args) { Size s=Size.SMALL; Si ...
- Centos7上vsftp脚本--> sh vsftp.sh 用户名 密码 --> sh vsftp.sh install
#!/bin/bash #vsftp install . /etc/rc.d/init.d/functions users=/etc/vsftpd/vftpuser.txt login=/etc/vs ...
- linux+jenkins+jmeter+ant持续集成
0.安装jdk 1.下载jdk8 登录网址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151 ...
- shell设置连接服务器永不超时
1.打开/etc/ssh/sshd_config vim /etc/ssh/sshd_config 2.设置如下内容: MaxAuthTries 60 MaxSessions 3 ClientAl ...
- python爬虫快递查询系统(源码)
import requestsimport json def get_express_type(postid): '''根据快递单号来智能判断快递类型''' url = 'http://www.kua ...
- POJ 1064 Cable master (二分法+精度控制)
Cable master Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65358 Accepted: 13453 De ...
- Redis设置内存最大占用值
Redis设置内存最大占用值: Redis设置占用物理机最大的内存 #占用最大20G maxmemory 20480mb Redis设置内存装不下了,有限删除即将过期的 当前已用内存超过maxmemo ...
- xxl系列部署启动通用办法
http://10.10.6.186:8080/xxl-job-admin # 编译mvn compile # 清理mvn clean # 打包mvn package # 先清理后编译mvn clea ...
- .NET成年了,然后呢?
作者|Lex Li 编辑|郭蕾 这可能是唯一一篇系统回顾 .NET 发展的文章..NET 的成年礼到了,你会送它什么? 2014 年 11 月 12 日,美国纽约曼哈顿,多云,气温适宜.微软公司执行副 ...