To rank the array into the smallest number

Source: Internet
Author: User
Tags java comparator

The title description enters an array of positive integers, stitching up all the numbers in the array into a number, and printing 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. This topic compares the size of the array composition character, so it is natural to think about the large number of problems. So the whole process is to use strings to compare and save. Train of thought: not a<b so ab<ba, because 3,31 spell 331>313, so we should set their own set of comparative rules, if ab<ba so called a<b, so we need to do is to the entire array according to our comparison rules from small to large arrangement. Then string them up from beginning to end. Because Java is not familiar with, so not with the comparator, but with the traditional bubbling, call yourself to build a comparison function comparison function is to string the two in the form of strings and then call Str1.compareto (str2) method, if the str1 is greater than str2 then return positive, otherwise return a negative value. On the Code
ImportJava.util.*; Public classSolution { PublicString Printminnumber (int[] numbers) {         for(inti=0;i<numbers.length;i++){             for(intj=numbers.length-1;j>i;j--){                if(Compare (Numbers[j-1],numbers[j]) >0){                    intTemp=numbers[j-1]; Numbers[j-1]=Numbers[j]; NUMBERS[J]=temp; }}} String ans="";  for(inti=0;i<numbers.length;i++) {ans+=Numbers[i]; }        returnans; }     Public intCompareintInt1,intInt2) {String str1=int1+ "" +Int2; String str2=int2+ "" +int1; returnStr1.compareto (STR2); }}

Comes with the Java comparator code, which calls the sort function directly, using a custom rule comparison, directly replacing the top bubbling process

New Comparator<integer>() {    publicint  Compare (Integer Str1,integer str2) {   String S1=str1+ "" +str2;   String S2=str2+ "" +str1;          return S1.compareto (s2);     }  });

To rank the array into the smallest number

Related Article

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.