The sword offer-the array into the smallest number.

Source: Internet
Author: User

Title Description: 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: If the array of digits are the same, you only need to follow the numbers from small to large splicing, so the number of bits to deal with the last one as a fill until the maximum number of digits, the final order can be, you need to define a class store processed numbers and the number in the original position of the array.

AC Code:

1 Importjava.util.ArrayList;2 Importjava.util.Arrays;3  Public classSolution {4      PublicString Printminnumber (int[] numbers) {5            if(numbers.length==0)6                return NewString ();7 Arrays.sort (numbers);8String []s=NewString[numbers.length];9          for(inti=0;i<numbers.length;i++){Tens[i]=string.valueof (Numbers[i]); One         } ANum ob[]=NewNum[s.length]; -         intN=s[s.length-1].length (); -          for(inti=0;i<s.length;i++){ the             if(S[i].length () <N) { -                 intx=n-s[i].length (); -                  for(intj=0;j<x;j++) -S[i]+=s[i].charat (S[i].length ()-1); +             } -ob[i]=Newnum (); +ob[i].index=i; Aob[i].x=integer.valueof (S[i]); at         } - arrays.sort (OB); -String str= ""; -          for(inti=0;i<ob.length;i++){ -str+=string.valueof (Numbers[ob[i].index]); -         } in         returnstr; -     } to     classNumImplementsComparable<num>{ +    intx; -    intindex; the @Override *      Public intcompareTo (num o) { $         //TODO auto-generated Method StubPanax Notoginseng         returnX-o.x; -     } the  +      A      the } +}

The sword offer-the array into the smallest number.

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.