Cracking The Coding Interview 9.1
生活随笔
收集整理的這篇文章主要介紹了
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]<<" ";}
}
轉載于:https://www.cnblogs.com/Vulkan/p/7530074.html
總結
以上是生活随笔為你收集整理的Cracking The Coding Interview 9.1的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【转】【Asp.Net】Asp.net发
- 下一篇: MFC VS2005 添加Overrid