Largest Number
Given a list of non negative integers, arrange them such that they form the largest number.
For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.
Note: The result may be very large, so you need to return a string instead of an integer.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
SOLUTION 1:
參考http://bookshadow.com/weblog/2015/01/13/leetcode-largest-number/的解法:
貪心思路:對於兩個備選數字a和b,如果str(a) + str(b) > str(b) + str(a),則a在b之前,否則b在a之前
按照此原則對原數組從大到小排序即可
時間復雜度O(nlogn)
易錯樣例:
Input: [0,0]
Output: "00"
Expected: "0"
JAVA CODE:

1 public class Solution { 2 public String largestNumber(int[] num) { 3 // 1045 4 // 1111 begin. 5 if (num == null) { 6 return null; 7 } 8 9 ArrayList<Integer> list = new ArrayList<Integer>(); 10 for (int n1: num) { 11 list.add(n1); 12 } 13 14 Collections.sort(list, new Comparator<Integer>(){ 15 public int compare(Integer o1, Integer o2) { 16 String s1 = "" + o1 + o2; 17 String s2 = "" + o2 + o1; 18 19 return s2.compareTo(s1); 20 } 21 }); 22 23 StringBuilder sb = new StringBuilder(); 24 for (int n: list) { 25 sb.append(n); 26 } 27 28 if (sb.charAt(0) == '0') { 29 return "0"; 30 } 31 32 return sb.toString(); 33 } 34 }
附上一些關於Comparator ,comparable 的解釋:
Comparator 是一個獨立的比較器,里面implements "compare",而Comparable 可以由class本身來implement。
http://stackoverflow.com/questions/4108604/java-comparable-vs-comparator
Java : Comparable vs Comparator [duplicate]
When your class implements Comparable, the compareTo method of the class is defining the "natural" ordering of that object. That method is contractually obligated (though not demanded) to be in line with other methods on that object, such as a 0 should always be returned for objects when the .equals() comparisons return true.
A Comparator is its own definition of how to compare two objects, and can be used to compare objects in a way that might not align with the natural ordering.
For example, Strings are generally compared alphabetically. Thus the "a".compareTo("b") would use alphabetical comparisons. If you wanted to compare Strings on length, you would need to write a custom comparator.
In short, there isn't much difference. They are both ends to similar means. In general implement comparable for natural order, (natural order definition is obviously open to interpretation), and write a comparator for other sorting or comparison needs.
相關reference:
http://examples.javacodegeeks.com/core-java/util/comparator/java-comparator-example/
CompareTo示例:
以下的小例子展示了CompareTo的用法:
Card 這個class 使用compareTo函數來實現內建的排序規則,輸出如下:
false
1
2
4

1 public static void main(String[] strs) { 2 int[] num = {1, 2}; 3 largestNumber(num); 4 5 ArrayList<Card> cards = new ArrayList<Card>(5); 6 Card card1 = new Card(1); 7 Card card2 = new Card(4); 8 Card card3 = new Card(2); 9 10 cards.add(card1); 11 cards.add(card2); 12 cards.add(card3); 13 14 if (card1.compareTo(card2) > 0) { 15 System.out.println("true"); 16 } else { 17 System.out.println("false"); 18 } 19 20 // uses compareTo method implemented in Card object to compare them 21 Collections.sort(cards); 22 23 // uses compare method implements in Comparator class 24 //Collections.sort(cards, new CompareBySuitRank()); 25 26 for (Card card : cards) 27 System.out.println(card.toString()); 28 29 } 30 31 public static class Card implements Comparable<Card>{ 32 int val; 33 34 public String toString() { 35 return "" + val; 36 } 37 38 public Card(int val) { 39 super(); 40 this.val = val; 41 } 42 43 public int compareTo(Card o) { 44 return this.val - o.val; 45 } 46 }
Github:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/string/LargestNumber.java