Interview Question 33: Make the array the smallest number

Source: Internet
Author: User
Package com.abuge;
 /** * Face Test 33: Put the array in the smallest number * Enter a positive integer array, put all the numbers in the array into a number, printing to be able to stitch out the smallest of all numbers.
* For example, the input array {3, 32, 321} will print the smallest number of three digits * thinking: * 1, the conversion of integer number to string, can solve the problem of invisible large number * 2, the development of new rules/import java.util.Arrays;

Import Java.util.Comparator; 
	Class Mycomprator implements comparator<string> {String result1 = null; 

	String result2 = null;
		@Override public int Compare (string str1, String str2) {result1 = str1+ str2;
	
		RESULT2 = str2 + str1;
	Return Result1.compareto (RESULT2); } public class Getminnum {public static String Getminnum (int[] A, int len) {if (a = = NULL | | | len <= 0) Retu
		
		RN null;
		string[] tmp = new String[len];
		Converts an array to a string for (int i = 0; i < len; i++) {Tmp[i] = string.valueof (a[i));
		Arrays.sort (TMP, New Mycomprator ()) sorted by the new rule;
		StringBuilder sb = new StringBuilder ();
		for (int i = 0; i < len; i++) {sb.append (tmp[i]);
	return sb.tostring (); public static void Main (string[] args) {int[] a = {456, 3, 1};
		
		int len = a.length;
	System.out.println (Getminnum (A, Len));
 }
}

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.