lintcode-Merge sorted array II

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.