Title Description: 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.
The point is to rewrite the comparator function
Code:
Importjava.util.ArrayList;ImportJava.util.Comparator;Importjava.util.Arrays; Public classSolution { PublicString Printminnumber (int[] numbers) { if(numbers==NULL|| numbers.length==0)return""; intlen=numbers.length; string[] Str=NewString[len]; StringBuffer SB=NewStringBuffer (); for(inti=0;i<len;i++) {Str[i]=string.valueof (Numbers[i]); } //overriding the comparator functionArrays.sort (str,NewComparator<string>(){ Public intCompare (string s1,string s2) {string C1=s1+S2; String C2=s2+S1; returnC1.compareto (C2); } }); for(inti=0;i<len;i++) {sb.append (str[i]); } returnsb.tostring (); }}
Interview question: Arrange the array into the smallest number