剑指offer P49 A,B皆排好序,把A插入B

来源:互联网 发布:淘宝直邮是真的吗 编辑:程序博客网 时间:2024/05/16 07:57
void MergeAToB(int A[],int lengthA,int B[],int lengthB,int totallengthB){if(A==NULL||B==NULL||lengthA<0||lengthB<0||totallengthB<lengthB||totallengthBB[curpointerB]&&curpointerA>=0){curpointerA--;}if(curpointerA!=lengthA-1){int k=1;for(int i=curpointerA+1;i<lengthA;i++){B[curpointerB+k]=A[i];k++;lengthB++;}//如果A的首位都比B末位大,结束if(curpointerA<0 return="" while="" curpointera="">=0){//若A的目前指针所指的值比B的首位要小,直接把A目前指针所指位置之前的值拷到B的首位前,结束if(A[curpointerA]0;i--){B[k]=B[i];k--;}for(int i=curpointerA;i>0;i--){B[i]=A[i];}lengthB=k+1;return;}//对于其他情况的处理while(A[curpointerA]=0){curpointerB--;}for(int i=lengthB;i>=curpointerB+2;i--){B[i]=B[i-1];}B[curpointerB+1]=A[curpointerA];lengthB++;curpointerA--;}return;}
0 0
原创粉丝点击