no.88 Merge Sorted Array

Source: Internet
Author: User

no.88 Merge Sorted Array

Given sorted integer Arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.

Note:
  nums1  has Enough Space (size is greater or equal To  m  +  n ) to the hold additional elements From  nums2 . The number of elements initialized In  nums1 and  nums2  are  m  and   n  respectively.

Tags: Array of pointers

//ask: Is there an error input? i.e. whether it is determined to be sorted and whether it needs to be detected by itself//ask: Is it ascending or descending?//The first time you do it, it's stuck, don't panic, it's simpleNote: The difference between the combined ordered array and the merged ordered single-linked list in the subject and merge order!!!
 1 #include "stdafx.h"2#include <iostream>3 using namespacestd;4 classSolution5 {6  Public:7     voidMergeintA[],intMintB[],intN)8     {9         if(M <0|| N <0)Ten         { One             return; A         } -  -         intindexa=m-1; the         intindexb=n-1; -         intIndexcurrent = m+n-1; -         //Suppose the sort is in ascending order -         //Suppose to be sort of!!  +         //Test instructions: Insert B into a, so from the rear to find "test instructions understand clearly!! " -          while(Indexb >=0&& indexcurrent>=0)//there is still data to be inserted +         { A             if (Indexa < 0) at                 Break ; -             if(A[indexa] >=B [INDEXB]) -a[indexcurrent--] = a[indexa--]; -             Else -a[indexcurrent--] = b[indexb--]; -         } in         //!! Forget it before.//and the difference between the combined ordered list -         While (indexb >= 0) to         { +             a[indexcurrent--] = b[indexb--]; -         } the     } * }; $ intMain ()Panax Notoginseng { - solution Sol; the //test 1:a null +     inta1[Ten];//special case: a[] A     intb1[4]= {1,3,5,6}; theSol.merge (A1,0, B1,4); +  for(auto p:a1) -cout << P <<" "; $cout <<Endl; $ //test 2:a long, B short -     inta2[Ten]= {0,1,4,7}; -     intb2[3]= {3,5,6}; theSol.merge (A2,4, B2,3); -  for(auto p:a2)Wuyicout << P <<" "; thecout <<Endl; - //test 3:a short, B long Wu     inta3[Ten]= {0,1,2}; -     intb3[4]= {3,4,4,6}; AboutSol.merge (A3,3, B3,4); $  for(auto p:a3) -cout << P <<" "; -cout <<Endl; -     return 0; A}

no.88 Merge Sorted Array

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.