To rank the array into the smallest number

Source: Internet
Author: User

Enter 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.

String.append adds a string or char at the end. String.Compare, compare,abcd>ab; one by one

IDEA: First, num is converted to string. The comparison of strings is then,str1+str2<str2+str1; to sort them. For num-turn string, use StringStream.

Another notation, string *str=new string[n],string array

1 classSolution {2  Public:3     stringPrintminnumber (vector<int>numbers) {4        5         intn=numbers.size ();6         stringRes;7         if(n<1)returnRes;8vector<string>str;9         stringtmp;Ten          for(intI=0; i<n;i++){ One StringStream stream; Astream<<Numbers[i]; -Stream>>tmp; - Str.push_back (TMP); the stream.clear (); -         } -          for(intI=0; i<n-1; i++){ -              for(intj=0; j<n-1-i;j++){ +                 if(Compare (str[j],str[j+1])) -Swap (str[j],str[j+1]); +             } A         } at          for(intI=0; i<n;i++) -res+=Str[i]; -         returnRes; -     } - Private: -     BOOLComparestringSTR1,stringstr2) { in         stringtmp1=str1.append (str2); -         stringTmp2=str2.append (str1); to         if(Tmp1.compare (TMP2) >0) +             return true; -         return false; the     } *};

To rank the array into 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.