Title Description:
Merges two sorted integer arrays A and b into a new array.
Precautions
You can assume that a has enough space (the size of a array is greater than or equal to m+n) to add the elements in B.
Sample Example
Give a = [1, 2, 3, empty, empty] , B =[4, 5]
After merging A will become[1,2,3,4,5]
classSolution {/** * @parama:sorted Integer array A which have m elements, * but size of a is m+n *@paramb:sorted integer array B which has n elements *@return: void*/ Public voidMergesortedarray (int[] A,intMint[] B,intN) {//Write your code here intI=0,j=0,q=0,p=0; if(m==0){ for(into=0;o<n;o++) A[m++] =B[o]; }Else{ intMinA = A[0],maxa = A[m-1]; for(q=0;q<n;q++) {//find out that the smallest number in B is smaller than a . if(B[q] >=MinA) Break; } for(p=0;p<n;p++) {//find out that the largest number in B is bigger than a . if(B[p] >=MaxA) Break; } if(q!=0) {//Put a number smaller than the smallest number in a in B to the front of a, the length of a increases for(into=m-1;o>=0;o--) A[o+Q] =A[o]; for(into=0;o<q;o++) A[o]=B[o]; M= m+Q; } if(P! = N) {//Put a number larger than the largest number in a in B to the last side of a, the length of a increases for(into=p;o<n;o++) A[m++] =B[o]; N=p; } for(i=0;i<m-1;i++){ for(j=q;j<n;j++){ if(B[j]>=a[i] && b[j]<a[i+1]){ intInsertnum = 1; for(intk=j+1;k<n;k++){ if(b[k]<=a[i+1]) Insertnum++; } //The elements in B are inserted in a for(into=m-1;o>i;o--) {A[o+insertnum] =A[o]; } for(intt=0;t<insertnum;t++) {A[i+1+t] = b[j+T]; } I= i+Insertnum; Q= q+Insertnum; M= m+Insertnum; Break; }Else{ Break; } } } } }}
lintcode-Merge sorted array II