天天看点

[LeetCode] Largest Number 最大组合数

Given a list of non negative integers, arrange them such that they form the largest number.

For example, given <code>[3, 30, 34, 5, 9]</code>, the largest formed number is <code>9534330</code>.

Note: The result may be very large, so you need to return a string instead of an integer.

Credits:

这道题给了我们一个数组,让我们将其拼接成最大的数,那么根据题目中给的例子来看,主要就是要给给定数组进行排序,但是排序方法不是普通的升序或者降序,因为9要排在最前面,而9既不是数组中最大的也不是最小的,所以我们要自定义排序方法。如果不参考网友的解法,我估计是无法想出来的。这种解法对于两个数字a和b来说,如果将其都转为字符串,如果ab &gt; ba,则a排在前面,比如9和34,由于934&gt;349,所以9排在前面,再比如说30和3,由于303&lt;330,所以3排在30的前面。按照这种规则对原数组进行排序后,将每个数字转化为字符串再连接起来就是最终结果。代码如下:

继续阅读