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.
Idea: If the array of digits are the same, you only need to follow the numbers from small to large splicing, so the number of bits to deal with the last one as a fill until the maximum number of digits, the final order can be, you need to define a class store processed numbers and the number in the original position of the array.
AC Code:
1 Importjava.util.ArrayList;2 Importjava.util.Arrays;3 Public classSolution {4 PublicString Printminnumber (int[] numbers) {5 if(numbers.length==0)6 return NewString ();7 Arrays.sort (numbers);8String []s=NewString[numbers.length];9 for(inti=0;i<numbers.length;i++){Tens[i]=string.valueof (Numbers[i]); One } ANum ob[]=NewNum[s.length]; - intN=s[s.length-1].length (); - for(inti=0;i<s.length;i++){ the if(S[i].length () <N) { - intx=n-s[i].length (); - for(intj=0;j<x;j++) -S[i]+=s[i].charat (S[i].length ()-1); + } -ob[i]=Newnum (); +ob[i].index=i; Aob[i].x=integer.valueof (S[i]); at } - arrays.sort (OB); -String str= ""; - for(inti=0;i<ob.length;i++){ -str+=string.valueof (Numbers[ob[i].index]); - } in returnstr; - } to classNumImplementsComparable<num>{ + intx; - intindex; the @Override * Public intcompareTo (num o) { $ //TODO auto-generated Method StubPanax Notoginseng returnX-o.x; - } the + A the } +}
The sword offer-the array into the smallest number.