《剑指offer》面试题4(合并数组)

来源:互联网 发布:网络风靡的性感骚彤彤 编辑:程序博客网 时间:2024/05/29 07:36
#include <iostream>using namespace std;void mergeArray(int *A1,int& length1, int*A2, int length2)//把A2合并到A1中,假设A1,A2中的元素是从小到大{    if(A1==NULL || A2==NULL || length1<0 || length2<0) return;    int p1 = length1-1, p2 = length2-1;    length1 += length2;    int p = length1-1;    while(p2>=0 && p1>=0)    {        A1[p--] = A1[p1]>A2[p2]?A1[p1--]:A2[p2--];    }    while(p2>=0)    {        A1[p--] = A2[p2--];    }    /*while(p1>=0)            //可不写    {        A1[p--] = A2[p1--];    }*/}int main(){    int* a = new int[5];    int lena = 2;    a[0] = 2;a[1] = 7;    int b[] = {3,8};    mergeArray(a,lena,b,2);    for(int i=0;i<lena;i++)        cout<<a[i]<<" ";    cout<<endl;    return 0;}

0 0
原创粉丝点击