To rank the array into the smallest number

Source: Internet
Author: User

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.
Import java.util.ArrayList;
Import Java.util.Comparator;
Import java.util.Collections;
Import java.util.*;
public class Solution {
Public String printminnumber (int [] numbers) {
String[] Str=new string[numbers.length];
StringBuilder sb=new StringBuilder ();
if (numbers.length<=0| | Numbers==null) return "";
for (int i=0;i<numbers.length;i++) {
Str[i]=string.valueof (Numbers[i]);
}
Arrays.sort (str,new comparator<string> () {

@Override//rewrite
public int Compare (String str1,string str2) {
String c1=str1+str2;
String c2=str2+str1;
return C1.compareto (C2);
}

});
for (int j=0;j<numbers.length;j++) {
Sb.append (Str[j]);
}
return sb.tostring ();
}

}

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.