leetcode [#179] | GCidea's blog
目录

题目
Given a list of non negative integers, arrange them such that they form the largest number.
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.
解决方案
1 | public class Solution { |
注意事项
1.曾考虑以下想法:
- 对于两个要比较的数,将长度补齐,并且较短的末尾是用0补齐
- 对于两个要比较的数,将长度补齐,并且较短的末尾是用个位数补齐
这两种方法都需要考虑不少特殊情况再单独做出判断,不是很完整。
2.上述方法是借鉴discuss中vote较高的回答。核心是实现Comparator接口用来定义排序规则,用此规则使用Arrays.sort排序。
3.由于最后要做字符串拼接,因此务必使用StringBuilder而非String直接相连,大大提高性能。