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