Given two sorted integer arrays A and B, merge B into A as one sorted array.

思路:

因为A的后面的部分都是空的留出来给我们放元素,所以最好是从后往前塞元素进去

    void mergeSortedArray(int A[], int m, int B[], int n) {
// write your code here
int i = m-;
int j = n-;
int index = m+n-;
while(i>=&&j>=){
if(A[i]>=B[j]){
A[index]=A[i];
index--;
i--;
}else{
A[index]=B[j];
index--;
j--;
}
}
while(i>=){
A[index--]=A[i--];
}
while(j>=){
A[index--]=B[j--];
}
}

[LintCode笔记了解一下]64.合并排序数组的更多相关文章

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

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

  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. lintcode 中等题:搜索旋转排序数组II

    题目 搜索旋转排序数组 II 跟进“搜索旋转排序数组”,假如有重复元素又将如何? 是否会影响运行时间复杂度? 如何影响? 为何会影响? 写出一个函数判断给定的目标值是否出现在数组中. 样例 给出[3, ...

  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. LintCode之合并排序数组

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

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

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

  9. [LintCode] 合并排序数组

    A subroutine of merge sort. class Solution { public: /** * @param A and B: sorted integer array A an ...

随机推荐

  1. java 字符串排序

    http://bbs.csdn.net/topics/280032929 大可不需要那样复杂了!(一)如果要排序的为字符串,如:String sortStr = "ACDFE";  ...

  2. Linux 为FTP 服务器添加iptables规则--案例分析

    一.故障描述 由于开发提出需求,为他们搭建内部ftp服务器,搭建好后,提交给他们,测试可以正常使用.后来过了一段时间后,有一天无法登陆了.于是去ftp主机上检查问题,ftp的配置文件没有改动,端口监听 ...

  3. System.Security.Cryptography.CryptographicException: 系统找不到指定的文件

    默认为false 改为true

  4. 【转】浅析VO、DTO、DO、PO的概念、区别和用处

    原文地址:http://blog.csdn.net/zjrbiancheng/article/details/6253232 概念: VO(View Object):视图对象,用于展示层,它的作用是把 ...

  5. sublime中开启表格插入

    1. 导入包 tableedit 2. ctrl+shift +p 输入teecv,然后开启后面连个东西: 3 按tab可以插入表格

  6. chrom中安装elastic search head插件

    1. 访问https://chrome.google.com/webstore/detail/elasticsearch-head/ffmkiejjmecolpfloofpjologoblkegm/ ...

  7. Oracel官网下载各类版本的JDK

    下载地址 http://www.oracle.com/technetwork/java/javase/downloads/index.html 拉到最下面 点download 在这里就可以下载到各个版 ...

  8. Synchronized块同步变量的误区

    我们可以通过synchronized块来同步特定的静态或非静态方法.要想实现这种需求必须为这些特性的方法定义一个类变量,然后将这些方法的代码用synchronized块括起来,并将这个类变量作为参数传 ...

  9. springboot 的定时任务使用

    定时任务在Spring Boot中的集成 在启动类中加入开启定时任务的注解: 在SpringBoot中使用定时任务相当的简单.首先,我们在启动类中加入@EnableScheduling来开启定时任务. ...

  10. TI XDC工具入门简介

    1. XDC(Express DSP Component)是TI提供的一个命令行工具,它可以生成并使用实时软件组件包,它包括一系列工具,这些工具可以允许你将你的C语言代码组织成类似于java的包管理方 ...