The title description enters an array of positive integers, stitching up all the numbers in the array into a number, and printing 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. This topic compares the size of the array composition character, so it is natural to think about the large number of problems. So the whole process is to use strings to compare and save. Train of thought: not a<b so ab<ba, because 3,31 spell 331>313, so we should set their own set of comparative rules, if ab<ba so called a<b, so we need to do is to the entire array according to our comparison rules from small to large arrangement. Then string them up from beginning to end. Because Java is not familiar with, so not with the comparator, but with the traditional bubbling, call yourself to build a comparison function comparison function is to string the two in the form of strings and then call Str1.compareto (str2) method, if the str1 is greater than str2 then return positive, otherwise return a negative value. On the Code
ImportJava.util.*; Public classSolution { PublicString Printminnumber (int[] numbers) { for(inti=0;i<numbers.length;i++){ for(intj=numbers.length-1;j>i;j--){ if(Compare (Numbers[j-1],numbers[j]) >0){ intTemp=numbers[j-1]; Numbers[j-1]=Numbers[j]; NUMBERS[J]=temp; }}} String ans=""; for(inti=0;i<numbers.length;i++) {ans+=Numbers[i]; } returnans; } Public intCompareintInt1,intInt2) {String str1=int1+ "" +Int2; String str2=int2+ "" +int1; returnStr1.compareto (STR2); }}
Comes with the Java comparator code, which calls the sort function directly, using a custom rule comparison, directly replacing the top bubbling process
New Comparator<integer>() { publicint Compare (Integer Str1,integer str2) { String S1=str1+ "" +str2; String S2=str2+ "" +str1; return S1.compareto (s2); } });
To rank the array into the smallest number