Cracking The Coding Interview 9.1
//原文:
//
// You are given two sorted arrays, A and B, and A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted order.
//
// 译文:
//
// A和B是两个有序数组(假设为递增序列),而且A的长度足以放下A和B中所有的元素, 写一个函数将数组B融入数组A,并使其有序。
#include <iostream>
using namespace std;
void mswap(int &a, int &b)
{
a = a^b;
b = a^b;
a = a^b;
} void merge(int *a, int asize, int *b, int bsize)
{
int ka= asize - 1;
int kab = asize + bsize - 1;
for (int kb = bsize - 1; kb >-1; kb--)
{
while (b[kb] < a[ka])
{
a[kab] = a[ka];
kab--;
ka--;
}
a[kab] = b[kb];
kab--;
}
} int main()
{
int a[20] = {4,5,9,11,14,22,33,34};
int b[5] = {1,3,7,10,12};
merge(a, 8, b, 5);
for (int i = 0;i <13; i++)
{
cout<<a[i]<<" ";
}
}
Cracking The Coding Interview 9.1的更多相关文章
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- 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 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目6
2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...
- 《Cracking the Coding Interview》——第5章:位操作——题目7
2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)
#include <iostream> #include <cstdio> #include <vector> #include <stack> #de ...
随机推荐
- 测试环境的好工具bginfo
省的自己来回找这台机器的IP,剩余空间了. 直接都显示在桌面了. https://www.howtogeek.com/school/sysinternals-pro/lesson7/
- Arduino 数字函数总结
Arduino 有三个数字函数,分别是:pinMode( ), digitalWrite( ),digitalRead( ).三个函数各有其作用,pinMode( ) 在初始化 setup( )函数中 ...
- boke练习: freemarker对空变量报错 (classic_compatible设置true,解决报空错误)
我有一个变量: commentModel 默认只是为空, 在freemarker模板中使用<#if>判断是报错 <#if commentModel> ..... </#i ...
- python3-知识扩展扫盲易忘-zip的用法
>>>a = [1,2,3] >>> b = [4,5,6]>>> c = [4,5,6,7,8]>>> zipped = zi ...
- 20180518VSTO多簿单表汇总
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsof ...
- ssh登陆报错:packet_write_wait: Connection to x.x.x.x port 22: Broken pipe
ssh登陆报错:packet_write_wait: Connection to x.x.x.x port 22: Broken pipe 参考文章: https://patrickmn.com/as ...
- linux中的软、硬链接
linux中的软.硬链接 硬链接 硬链接(hard link),如果文件B是文件A的硬链接,则A的inode节点号与B的inode节点号相同,即一个inode节点对应两个不同的文件名,两个文件名指向同 ...
- python记录_day10 动态传参 命名空间 作用域
一.动态传参 动态传参用到 *args 和 **kwargs ,*号表示接收位置参数,args是参数名:**表示接收关键字参数,kwargs是参数名 def chi(*food): print(foo ...
- const typedef 和指针的问题(这里必须初始化的才初始化了,不必须的则没有初始化)
这里很容易搞混: tyepdef double dou;//这里是dou是double的别名 #include<iostream> using namespace std; int mai ...
- thinkphp或者kohana 导入和读取文件
1.无论是那个框架的导入,其实都是一样的原理的,但是首先我们要导入包,可能就这点不同. kohana的导入包的方法:require_once(Kohana::find_file('vendor','P ...