數組拼接成最小的數字

數組拼接成最小的數字

package com.dugstudio.SwordToOffer;

import jdk.nashorn.internal.ir.IdentNode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 輸入一個正整數數組,把數組裏所有數字拼接起來排成一個數,打印能拼接出的所有數字中最小的一個。
 * 例如輸入數組{3,32,321},則打印出這三個數字能排成的最小數字爲321323。
 */
public class PrintMinNumber {

    public class Solution {
        public String PrintMinNumber(int [] numbers) {
            int len=numbers.length;
            List<Integer > list=new ArrayList<Integer>();
            int min=Integer.MAX_VALUE;
            int x;
            for (int i=0;i<len;i++){
                list.add(numbers[i]);
            }
            Collections.sort(list, new Comparator<Integer>() {
                public int compare(Integer o1, Integer o2) {
                    String s1=o1+""+o2;
                    String s2=o2+""+o1;
                    return s1.compareTo(s2);
                }
            });
            String s="";
            for (int i:list){
                s+=i;
            }
            return s;
            }
        }
}
發佈了91 篇原創文章 · 獲贊 4 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章