2014-03-21 20:35

题目:给定已升序排列的数组A和数组B,如果A有足够的额外空间容纳A和B,请讲B数组合入到A中。

解法:由后往前进行归并。

代码:

 // 11.1 Given two sorted array A and B, suppose A is large enough to hold them both. Merge B into A.
#include <algorithm>
#include <cstdio>
using namespace std; void mergeBIntoA(int a[], int b[], int na, int nb)
{
if (na <= || nb <= ) {
// invalid parameter
return;
} int *pmin, *pmax;
pmin = min(a, b);
pmax = max(a + na + nb, b + nb);
if (pmax - pmin < na + nb + nb) {
// the memories overlap
return;
} int i, j, k; i = na;
j = nb;
k = na + nb; while (i > && j > ) {
if (a[i - ] > b[j - ]) {
a[--k] = a[--i];
} else {
a[--k] = b[--j];
}
}
while (j > ) {
a[--k] = b[--j];
}
} int main()
{
int *a, *b;
int na, nb;
int i; while (scanf("%d%d", &na, &nb) == && (na > && nb > )) {
a = new int[na + nb];
b = new int[nb];
for (i = ; i < na; ++i) {
scanf("%d", &a[i]);
}
for (i = ; i < nb; ++i) {
scanf("%d", &b[i]);
}
mergeBIntoA(a, b, na, nb);
for (i = ; i < na + nb; ++i) {
printf((i == ? "%d" : " %d"), a[i]);
}
putchar('\n'); delete[] a;
delete[] b;
a = nullptr;
b = nullptr;
} return ;
}

《Cracking the Coding Interview》——第11章:排序和搜索——题目1的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  5. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  6. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  7. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  8. 《Cracking the Coding Interview》——第11章:排序和搜索——题目4

    2014-03-21 21:28 题目:给定一个20GB大小的文本文件,每一行都是一个字符串.请设计方法将这个文件里的字符串排序. 解法:请看下面的注释. 代码: // 11.4 Given a fi ...

  9. 《Cracking the Coding Interview》——第11章:排序和搜索——题目3

    2014-03-21 20:55 题目:给定一个旋转过的升序排序好的数组,不知道旋转了几位.找出其中是否存在某一个值. 解法1:如果数组的元素都不重复,那么我的解法是先找出旋转的偏移量,然后进行带偏移 ...

  10. 《Cracking the Coding Interview》——第11章:排序和搜索——题目2

    2014-03-21 20:49 题目:设计一种排序算法,使得anagram排在一起. 解法:自定义一个comparator,使用额外的空间来统计字母个数,然后比较字母个数. 代码: // 11.2 ...

随机推荐

  1. VirtualBox虚拟机 host/guest 拷贝粘贴,共享剪贴板,安装guest additions

    Oracle VirtualBox 虚拟机,为了在主机.从机间拷贝文件,共享剪贴板,需要进行设置,以及安装guest additions软件 测试环境 host: windows 7 professi ...

  2. AttributeError: module 'requests' has no attribute 'get' 遇到了这个错误,是因为我把python关键字做了包名。。。

    初学者总会犯各种低级错误,这是其一,特此记录.

  3. Oracle数据库几种启动方式及查询当前状态

    Oracle数据库几种启动方式 1.startup nomount: 非安装启动,这种方式下启动可执行:重建控制文件.重建数据库,读取init.ora文件,启动instance,即启动SGA和后台进程 ...

  4. Windows核心编程-作业

    原文链接:http://zhujiangtao.com/?p=983 作业 作业 一个简单例程 CreateJobObject 创建作业 作业限制和 SetInformationJobObject A ...

  5. bootstarp v3 学习简记

    1.快速设置浮动通过这两个class让页面元素左右浮动. !important被用来避免某些问题. <div class="pull-left">...</div ...

  6. google detection

    paper: Scalable, High-Quality Object Detection ILSVRC14上,detection刷到55.7%的MAP,google插入的地方,别人倍感压力啊. 总 ...

  7. vue-wechat-title

    html中的title安装:npm install vue-wechat-title --save1.在mian.js中//网页titleimport VueTitle from 'vue-wecha ...

  8. java自定义泛型 面试题:接收任意数组进行反转 泛型通配符

    不用泛型只能操作某种类型进行反转 代码如下: package com.swift.fanxing; import org.junit.Test; public class RenyiReverse { ...

  9. OCCI的迭代修改

    传统的在执行多行DML(INSERT.UPDATE.DELETE)时,我们是多次调用executeUpdate():注意!当我们调用一次此函数时,则执行一次网络往返,当数据量大时则效率非常低.不过 O ...

  10. 执行pip命令时遇到 Fatal error in launcher: Unable to create process using '"'

    电脑同时安装了python-2.7.13跟python-3.6.1,安装时勾选了pip,环境变量也已经配置好. 为了方便运行,同时修改了可执行文件为 python2和python3.此时在cmd命令行 ...