描述

合并两个排序的整数数组A和B变成一个新的数组。

你可以假设A具有足够的空间(A数组的大小大于或等于m+n)去添加B中的元素。

您在真实的面试中是否遇到过这个题?

样例

给出 A = [1, 2, 3, empty, empty], B = [4, 5]

合并之后 A 将变成 [1,2,3,4,5]

public class Solution {
/*
* @param A: sorted integer array A which has m elements, but size of A is m+n
* @param m: An integer
* @param B: sorted integer array B which has n elements
* @param n: An integer
* @return: nothing
*/
public void mergeSortedArray(int[] A, int m, int[] B, int n) {
// write your code here int totalLength = m+n; while (n != 0 && m != 0) {
int curA = A[m - 1];
int curB = B[n - 1];
if (curA > curB) {
A[totalLength - 1] = curA;
m --;
} else {
A[totalLength - 1] = curB;
n --;
}
totalLength --;
}
//如果数组A的长度为0;前面的直接不执行
while (n != 0) {
A[totalLength - 1] = B[n - 1];
n--;
totalLength--;
}
// while (aSize != 0) { // } }
}
public class Solution {
/*
* @param A: sorted integer array A which has m elements, but size of A is m+n
* @param m: An integer
* @param B: sorted integer array B which has n elements
* @param n: An integer
* @return: nothing
*/
public void mergeSortedArray(int[] A, int m, int[] B, int n) {
if(n == 0){
return;
}
if(m == 0){
for(int l = 0 ; l < n ; l++){
A[l] = B[l];
}
return;
}
int[] C = new int[m + n]; int i = 0;
int j = 0;
int num = 0;
boolean flag = false;
for(; i < m ; ){
// write your code here
for(; j < n ; ){
if(A[i] < B[j]){
C[num] = A[i];
num++;
i++;
if(i == m){
flag = true;
break;
}
}else{
C[num] = B[j];
j++;
num++;
if(j == n){
flag = true;
break;
}
}
} if(flag){
break;
}
} if(i != m){
for(; i < m ; i++){
C[num] = A[i];
num++;
}
} if(j != n){
for(; j < n ; j++){
C[num] = B[j];
num++;
}
} if(num == m + n){
for(int l = 0 ; l < m+n ; l++){
A[l] = C[l];
} return;
} }
}

64. 合并排序数组.md的更多相关文章

  1. [LintCode笔记了解一下]64.合并排序数组

    Given two sorted integer arrays A and B, merge B into A as one sorted array. 思路: 因为A的后面的部分都是空的留出来给我们 ...

  2. lintcode:合并排序数组 II

    题目: 合并排序数组 II 合并两个排序的整数数组A和B变成一个新的数组. 样例 给出A = [1, 2, 3, empty, empty] B = [4,5] 合并之后A将变成[1,2,3,4,5] ...

  3. lintcode:合并排序数组

    题目: 合并排序数组 合并两个排序的整数数组A和B变成一个新的数组. 样例 给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6] 挑战 你能否优化你的算法,如果 ...

  4. LintCode之合并排序数组II

    题目描述: 分析:题目的意思是把数组A和数组B合并到数组A中,且数组A有足够的空间容纳A和B的元素,合并后的数组依然是有序的. 我的代码: public class Solution { /* * @ ...

  5. 6. 合并排序数组 II

    6. Merge Two Sorted Arrays Description Merge two given sorted integer array A and B into a new sorte ...

  6. 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之后,然后 ...

  7. 怎样合并排序数组(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 ...

  8. leetCode 88.Merge Sorted Array (合并排序数组) 解题思路和方法

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: Y ...

  9. LintCode之合并排序数组

    题目描述: 我的代码: public class Solution { /* * @param A: sorted integer array A * @param B: sorted integer ...

随机推荐

  1. LeetCode(93): 复原IP地址

    Medium! 题目描述: 给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式. 示例: 输入: "25525511135" 输出: ["255.255. ...

  2. mac 显示/不显示"任何来源"_ mac打开安装文件显示文件破损解决办法

    系统: macOS_10.12 导致文件破损原因: 软件有经过了汉化或者破解,所以可能被Mac认为「已损坏」 解决问题办法: 系统偏好设置 -> 安全性与隐私 -> 通用 -> 选择 ...

  3. easyui之自定义字体图标(鼠标覆盖时切换颜色)

    项目要求是自定义字体图标,使用easyui框架结构,众所周知easyui强功能弱样式,字体图标其实就是一张图片.要达到切换图标颜色的效果,要么就是有两套图,使用js控制.但是我这个人比较懒,不喜欢做复 ...

  4. 步步为营-89-SQL语句(删除重复数据)

    1:删除重复数据 --第一步:先找到重复数据 select ProcInstID from record_errorlog group by ProcInstID having count(ProcI ...

  5. Lua中assert( )函数的使用

    当Lua遇到不期望的情况时就会抛出错误,比如:两个非数字进行相加:调用一个非函数的变量:访问表中不存在的值等.你也可以通过调用error函数显示的抛出错误,error的参数是要抛出的错误信息. ass ...

  6. 51 nod 1212 无向图最小生成树(Kruckal算法/Prime算法图解)

    1212 无向图最小生成树 N个点M条边的无向连通图,每条边有一个权值,求该图的最小生成树. 收起 输入 第1行:2个数N,M中间用空格分隔,N为点的数量,M为边的数量.(2 <= N < ...

  7. Django中间件 及 form 实现用户登陆

    Django中间件 及 form 实现用户登陆 Form 验证 密码调用md5 加密存储 form.add_error("字段名", "错误信息") 自定义错误 ...

  8. 公共语言运行时支持(/clr)

    项目属性 -> 配置属性 -> “常规”里开启“公共语言运行时支持(/clr)

  9. 【BZOJ3514】Codechef MARCH14 GERALD07加强版 LCT+主席树

    题解: 还是比较简单的 首先我们的思路是 确定起点 然后之后贪心的选择边(也就是越靠前越希望选) 我们发现我们只需要将起点从后向前枚举 然后用lct维护连通性 因为强制在线,所以用主席树记录状态就可以 ...

  10. [转]解决-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment variable and mvn script match.

    来源:http://www.cnblogs.com/sprinng/p/5141233.html 1.添加M2_HOME的环境变量 2.Preference->Java->Installe ...