Title: 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: A sort, rule is two numbers combined up small in front.
Implementation code:
ImportJava.util.*; Public classSolution { PublicString Printminnumber (int[] numbers) { if(Numbers = =NULL|| Numbers.length <= 0) return NewString (); Integer[] Nums=NewInteger[numbers.length]; for(inti=0; i<numbers.length; i++) {Nums[i]=NewInteger (Numbers[i]); } arrays.sort (Nums,NewComparator<integer>() {@Override Public intCompare (integer O1, integer o2) {intVal1 = Integer.parseint (string.valueof (O1) +string.valueof (O2)); intVal2 = Integer.parseint (string.valueof (O2) +string.valueof (O1)); returnVal1 > Val2? 1:-1; } }); StringBuilder ret=NewStringBuilder (); for(inti=0; i<nums.length; i++) {ret.append (nums[i]); } returnret.tostring (); }}
To rank the array into the smallest number