描述

合并两个排序的整数数组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. spring boot 配置文件

    spring boot使用一个全局配置文件:主要是以下两种类型 application.properties  :例:server.port=9998 application.yml(YAML)  : ...

  2. 使用git克隆项目、从dev分支上更新代码以及将代码提交到Coding(或GitHub)上面

    本教程的目的: 这是个crm项目中,有两个分支一个是master 和 dev ,master主分支,不允许提交代码,我要拉去dev分支上最新的代码,并将修改后的项目,在推送到dev分支上. 一. 1. ...

  3. stylus入门教程,在webstorm中配置stylus

    转载:https://www.cnblogs.com/wenqiangit/p/9717715.html#undefined   stylus特点 富于表现力.具有健壮性.功能丰富.动态编码 不需要写 ...

  4. Mysql 5.7 CentOS 7 安装MHA

    Table of Contents 1. MHA简介 1.1. 功能 1.2. MHA切换逻辑 1.3. 工具 2. 环境 2.1. 软件 2.2. 环境 3. Mysql 主从复制 3.1. Mys ...

  5. RFC2119:RFC协议动词含义

    协议地址:http://www.ietf.org/rfc/rfc2119.txt MUST 必须的.通过它描述的对象,是强制要求的.它与REQUIRED和SHALL含义相同. MUST NOT 不允许 ...

  6. Aho-Corasick算法实现(简单关键字过滤)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  7. [转] Mongoose 参考手册

    Mongoose 参考手册 标签(空格分隔): MongoDB Mongoose 是什么? 一般我们不直接用MongoDB的函数来操作MongoDB数据库 Mongose就是一套操作MongoDB数据 ...

  8. javascript OOP实例—探测器

    <script> /*所有探测器都有探测的方法和分析的方法,分析当前的浏览器环境,不管是浏览器还是nodejs*/ /*container容器探测器*/ /*link链接探测器*/ /*外 ...

  9. ajax后台输出有红点

    转自 百度了很多,说有utf8的bom头,通过dw,sublime软件,各种清除格式无果. 后来直接在返回结果之前,执行一下ob_clean(); 完美解决问题

  10. 清北合肥day1

    题目: 1.给出一个由0,1组成的环 求最少多少次交换(任意两个位置)使得0,1靠在一起 n<=1000 2.两个数列,支持在第一个数列上区间+1,-1 每次花费为1 求a变成b的最小代价 n& ...