The sword is an offer: the array is the smallest number "45"

Source: Internet
Author: User

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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.