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 跟进"搜索旋转排序数组",假如有重复元素又将如何? 是否会影响运行时间复杂度? 如何影响? 为何会影响? 写出一个函数判断给定的目标值是否出现在数组中 ...
随机推荐
- Python基础之面向过程编程
要求:在文件里递归找到关于包含“Python”内容的文件的绝对路径并打印出来 #定义阶段 import os,time def init(func): #装饰器的作用是使下面的生成器初始化,yield ...
- Java基础之多线程框架
一.进程与线程的区别 1.定义: 进程是具有一定独立功能的程序关于某个数据集合上的一次运行活动,进程是系统进行资源分配和调度的一个独立单位. 线程是进程的一个实体,是CPU调度和分派的基本单位,它是比 ...
- base64解密
问题 : base64解密 时间限制: 1 Sec 内存限制: 128 MB 题目描述 Base64编码要求把3个8位字节(3*8=24)转化为4个6位的字节(4*6=24),之后在6位的前面补两个 ...
- Vue2 异步获取的数据(通过ajax)获取的数据 渲染到dom上
页面dom结构如下 <ul class="user" id="app"> <li><span>姓名: </span&g ...
- ubuntu 下配置munin
环境: "Ubuntu 13.10" 安装: apt-get install munin munin-nodeapt-get install apache2 配置: 1. vim ...
- windows10的文件浏览器中无法搜索文件内容
系统:更新到最新的win10(2018年8月23日 23:54:31) 重现步骤:git clone一个项目,然后切换到它的另一个分支:打开文件夹浏览器(explorer),在右上角里输入想要查找的字 ...
- ES优化总结
ES优化总结(特别是在bulk大量数据到ES的时候) https://blog.csdn.net/chenxun_2010/article/details/78602795 将 ELASTICSEAR ...
- OSGi HelloWorld
1.创建项目 2.Debug Configurations,配好之后,可以点一下右下角的Validate Bundles验证一下是否有问题 3.Debug
- C 语言的 GCC 扩展
GNU 编译器(GCC)提供了很多 C 语言扩展,编译器会使用该信息生成更高效的机器代码. 内联函数 static inline __attribute__ ((always_inline)) int ...
- signal() 和 sigaction()
[摘自<Linux/Unix系统编程手册>] Unix系统提供了两种方式来改变信号处置:signal() 和 sigaction(). signal() 的行为在不同Unix实现间存在差异 ...