java-68-把数组排成最小的数。一个正整数数组,将它们连接起来排成一个数,输出能排出的所有数字中最小的。例如输入数组{32, 321},则输出32132

import java.util.Arrays;
import java.util.Comparator;

public class MinNumFromIntArray {

	/**
	 * Q68输入一个正整数数组,将它们连接起来排成一个数,输出能排出的所有数字中最小的一个。
	 * 例如输入数组{32,  321},则输出这两个能排成的最小数字32132。请给出解决问题的算法,并证明该算法。
	 */
	public static void main(String[] args) {
		int[][] val={
				{32,321},//32132
				{532,45,78},//4553278
				{2,23,231},//223123
				{2,3,1},//123
		};
		for(int[] x:val){
			//solution 1
			String result=minNumFromIntArray(x);
			System.out.println(result);
			//solution 2
			AuxClass minStr=new AuxClass();
			minNumFromIntArray2(x,0,x.length-1,minStr);
			System.out.println(minStr.str);
		}
		
		//System.out.println("32".compareTo("321"));//<0
	}
	
	/*solution 1: 
	 * if ab<ba,then a<b
	 * 1.sort the integer array. the comparator is "if ab<ba,then a<b"
	 * 2.append each integer in the array to create a string and that's the result.
	 */
	public static String minNumFromIntArray(int[] x){
		String[] strs=stringsOf(x);
		Arrays.sort(strs,new Comparator<String>(){
			public int compare(String o1, String o2) {
				return (o1+o2).compareTo(o2+o1);
			}
		});
		StringBuilder sb=new StringBuilder();
		for(String each:strs){
			sb.append(each);
		}
		return sb.toString();
	}
	
	//int[] to String[].For comparing.	
	public static String[] stringsOf(int[] x){
		int len=x.length;
		String[] strs=new String[len];
		for(int i=0;i<len;i++){
			strs[i]=""+x[i];
		}
		return strs;
	}
	
	/*solution 2
	 * get all the permutations.
	 * find the min.
	 * Of course we use String instead of Big Integer.
	 */
	public static void minNumFromIntArray2(int[] x,int first,int last,AuxClass minStr){
		if(first==last){
			StringBuilder sb=new StringBuilder();
			for(int each:x){
				sb.append(each);
			}
			if(minStr.str==null){
				minStr.str=sb.toString();
			}else{
				if(minStr.str.compareTo(sb.toString())>0){
					minStr.str=sb.toString();
				}
			}
			return;
		}
		for(int i=first;i<=last;i++){
			swap(x,first,i);
			minNumFromIntArray2(x,first+1,last,minStr);
			swap(x,first,i);
		}
	}
	public static void swap(int[] x,int i,int j){
		int temp=x[i];
		x[i]=x[j];
		x[j]=temp;
	}
	
	/*
	 * when you use 'String' instead of inner class,you get 'null' all the time
	 */
	static class AuxClass{
		String str;
	}
}

你可能感兴趣的:(java)