Cracking The Coding Interview 9.1
2014-04-23 13:50
253 查看
//原文: // // 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》——第1章:数组和字符串——题目1
- 渣基础:比照Hawstein学Cracking the coding interview(1)
- Cracking the Coding Interview Q2.1
- 《Cracking the Coding Interview》——第4章:树和图——题目3
- 《Cracking the Coding Interview》——第6章:智力题——题目1
- 渣基础:比照Hawstein学Cracking the coding interview(2)
- 《Cracking the Coding Interview》——第6章:智力题——题目4
- 《Cracking the Coding Interview》——第9章:递归和动态规划——题目7
- Cracking the coding interview--Q15.1
- 《Cracking the Coding Interview》——第11章:排序和搜索——题目3
- Cracking the coding interview--Q15.3
- Cracking The Coding Interview 1.3
- Cracking the coding interview--问题与解答
- Cracking The Coding Interview3.3
- Cracking the coding interview--Q5.2
- Cracking the coding interview--Q20.1
- Cracking the coding interview--Q20.6
- 《Cracking the Coding Interview》——第12章:测试——题目2
- 《Cracking the Coding Interview》——第13章:C和C++——题目8
- 《Cracking the Coding Interview》——第16章:线程与锁——题目1