编写一个能将给定非负整数列表中的数字排列成最大数字的函数。例如,给定[50,2,1,9],最大数字为95021。
生活随笔
收集整理的這篇文章主要介紹了
编写一个能将给定非负整数列表中的数字排列成最大数字的函数。例如,给定[50,2,1,9],最大数字为95021。
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
編寫一個能將給定非負整數列表中的數字排列成最大數字的函數。例如,給定[50,2,1,9],最大數字為95021。
/*** This algorithm offers guaranteed n*log(n) performance.* * @param array* @return*/ public static String getLargestNumByArranged(Integer[] array) {Arrays.sort(array, new Comparator<Object>() {/**** 默認是從小到大排序 if the result > 0 then swap*/public int compare(Object o1, Object o2) {String left = o1.toString();String right = o2.toString();// 按字典順序比較 if the result > 0 then * -1return (left + right).compareTo(right + left) * -1;// return (right + left).compareTo(left + right);}});StringBuffer sb = new StringBuffer();for (Integer integer : array) {sb.append(integer.toString());}return sb.toString(); }public static void main(String[] args) {Integer[] VALUES = { 50, 2, 100, 99, 5, 7, 51, 50, 11 };System.out.println(getLargestNumByArranged(VALUES)); }本文參考:
http://www.genshuixue.com/i-cxy/p/8018152
總結
以上是生活随笔為你收集整理的编写一个能将给定非负整数列表中的数字排列成最大数字的函数。例如,给定[50,2,1,9],最大数字为95021。的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SLAM大牛Cyrill 开源SuMa
- 下一篇: R手册(Common)--R语言入门