The "45" topic description of the minimum number of the array
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, if the input array is {3.32.321}, the smallest number that the printed 3 numbers can stitch together is 321323.
A brief approach to methodology
The most violent method is to force all the numbers in the array to be violent, and then find the smallest one. But obviously we can't do that.
We should define a collation that:
先将整型数组转换成String数组,然后将String数组排序,最后将排好序的字符串数组拼接出来。关键就是制定排序规则。
* 排序规则如下:
* 若ab > ba 则 a > b,
* 若ab < ba 则 a < b,
* 若ab = ba 则 a = b;
* 解释说明:
* 比如
"3"
<
"31"
但是
"331"
>
"313"
,所以要将二者拼接起来进行比较.
In conclusion, to make the array elements a minimum, we can sort the elements of the array in the order of the minimum values, and the collation of the minimum arrangement is to put the elements in the case with the lower 22 combined value in front, We can imagine this process in a bubble sort order .
Java code
Import Java.util.arrays;import Java.util.comparator;import Java.util.scanner;public class Numarrtominnum {public stat IC void Main (string[] args) {Scanner input = new Scanner (system.in); String line =input.nextline (); String[]arr =line.split (""); Arrays.sort (arr, new comparator<string> () {@Override public int compare (string O1, String O2) {return (O1+O2). CompareTo (O2+O1); } }); String re= ""; for (String Str:arr) re+=str; System.out.println (re); } public String Printminnumber (int [] numbers) {String[]arr =new string[numbers.length]; for (int i=0;i<arr.length;i++) arr[i]=string.valueof (Numbers[i]); Arrays.sort (arr, new comparator<string> () {@Override public int compare (string O1, String O2) {return (O1+O2). CompareTo (O2+O1); } }); String re= ""; ForString Str:arr) Re+=str; return (re); }}
The sword is an offer: the array is the smallest number "45"