32, the sword refers to offer--the array into the smallest number

Source: Internet
Author: User

Title Descriptionenter an array of positive integers, combine all the numbers in the array into a number, and print the smallest of all the numbers that can be stitched together. For example, enter the array {3,32,321}, then print out the minimum number that these three numbers can be ranked as 321323. problem-solving ideas: the subject of MN and NM, which is how small line1) First copy each number into the strname in the form of a string2) Sort the call to it. Sort by the Compare rules we designed to compare3) Put the sorted array into the result and return the result
1 BOOLCompareConst string&st1,Const string&st2)2 {3     stringS1 = St1 +St2;4     stringS2 = St2 +St1;5     returnS1 <S2;6 }7 classSolution {8  Public:9     Const intG_maxnumberlength=Ten;Ten     stringPrintminnumber (vector<int>numbers) { One         stringresult; A         intLength =numbers.size (); -         if(Length <=0) -             returnresult; the         Char* * strnumbers=New Char*[length]; -          for(intI=0; i<length;i++) -         { -strnumbers[i]=New Char[g_maxnumberlength+1]; +sprintf (Strnumbers[i],"%d", Numbers[i]); -         } +Sort (strnumbers,strnumbers+length,compare); A          for(intI=0; i<length;i++) at result.append (Strnumbers[i]); -   -          for(intI=0; i<length;i++) -             Delete[] strnumbers[i]; -         Delete[] strnumbers; -         returnresult; in     } -};

32, the sword refers to offer--the array to the smallest number

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.