64-合并排序数组 II

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

注意事项

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

样例

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

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

标签

数组 排序数组 脸书

思路

从后向前遍历数组

code

class Solution {
public:
/**
* @param A: sorted integer array A which has m elements,
* but size of A is m+n
* @param B: sorted integer array B which has n elements
* @return: void
*/
void mergeSortedArray(int A[], int m, int B[], int n) {
// write your code here
int size = m + n, i = 0, a = m-1, b = n-1;
for(i=size-1; i>=0; i--){
if(A[a] <= B[b]) {
A[i] = B[b];
b--;
}
else {
A[i] = A[a];
a--;
}
}
}
};

lintcode-64-合并排序数组 II的更多相关文章

  1. LintCode之合并排序数组II

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

  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=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6] 1.Python:先将数组B加到数组A之后,然后 ...

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

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

  6. [LintCode] 合并排序数组II

    class Solution { public: /** * @param A: sorted integer array A which has m elements, * but size of ...

  7. 64. 合并排序数组.md

    描述 合并两个排序的整数数组A和B变成一个新的数组. 你可以假设A具有足够的空间(A数组的大小大于或等于m+n)去添加B中的元素. 您在真实的面试中是否遇到过这个题? 样例 给出 A = [1, 2, ...

  8. 6. 合并排序数组 II

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

  9. LintCode 6---合并排序数组 II

    import java.util.Arrays; public class Lint6 { /* * 合并两个排序的整数数组A和B变成一个新的数组.新数组也要有序. */ public static ...

  10. [容易]合并排序数组 II

    题目来源:http://www.lintcode.com/zh-cn/problem/merge-sorted-array/

随机推荐

  1. 获取APP地图权限

    获取APP地图权限 NSLocationWhenUseUsageDescription,在info里面设置为空

  2. (mybatis)There is no getter for property named 'isEffective' in 'class java.lang.String

    原来代码: <select id="findSpecialOffer" resultType="com.lizard.back.model.SpecialOffer ...

  3. djano-模板层基础知识

    ########模板层######## 模板层其实就是templates文件夹里的html文件 其实这里的每个html不是真正意义的上html代码,只有经过模板渲染过后才算的上真正的html页面. 一 ...

  4. Vue--- Vue(Pubsub + Ajax) 数据交互

    案例知识点 兄弟组件儿的通信     使用了Pubsub    订阅与发布 ajax数据请求    获取前   获取中   获取后   获取为空    获取异常 获取成功后显示数据给到  原先定义号的 ...

  5. Plupload使用API

    Plupload有以下功能和特点: 1.拥有多种上传方式:HTML5.flash.silverlight以及传统的<input type=”file” />.Plupload会自动侦测当前 ...

  6. 第13届景驰-埃森哲杯广东工业大学ACM程序设计大赛--A-跳台阶

    链接:https://www.nowcoder.com/acm/contest/90/A 来源:牛客网 1.题目描述 小明在坐景驰科技研发的无人车到达了目的地. 景驰科技(JingChi.ai)是一家 ...

  7. 免安装版Tomcat9中间件的安装

    [环境准备] OS版本:Windows10.64位 Tomcat版本:apache_tomcat9.0.7.zip免安装版 [彻底卸载已安装的Tomcat中间件] 01:由于是免安装版本,因此直接删除 ...

  8. git设置.gitignore文件

    .gitignore用来忽略某些git仓库中不需要上传到远程仓库的文件,例如target目录.下面说一下步骤. 1.在项目根目录中通过右键Git Bash,打开控制命令台,新建一个.gitignore ...

  9. Vue.js中 computed 和 methods 的区别

    官方文档中已经有对其的解释了,在这里把我的理解记录一下Vue中的methods.watch.computed computed 的使用场景 HTML模板中的复杂逻辑表达式,为了防止逻辑过重导致不易维护 ...

  10. h5移动端页面meta标签

    <!DOCTYPE html> <!-- 使用 HTML5 doctype,不区分大小写 --> <html lang="zh-cmn-Hans"&g ...