51:把数组排成最小的数

把数组排成最小的数

public class Offer51 {
	//思路:进行一个类似冒泡排序的方法来排序即可
    public String PrintMinNumber(int [] numbers) {
        if(numbers==null || numbers.length<=0)
            return "";
        ArrayList<String> list = new ArrayList<String>();
        for(int number:numbers)
            list.add(String.valueOf(number));
        //之前是把int转成string放到list中
        Collections.sort(list,new Comparator<String>(){
            @Override
            public int compare(String s1,String s2){
                String a=s1+s2;
                String b=s2+s1;
                return a.compareTo(b);
            }
        });

        System.out.println(list);

        StringBuilder sb= new StringBuilder();
        for(String str:list)
            sb.append(str);
        return sb.toString();
    }

    public static void main(String[] args) {
        int[] data = {1,4,224,1};
        String s = new Offer51().PrintMinNumber(data);
        System.out.println(s);
    }
}

©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页