# 179. Largest Number

Given a list of non-negative integers nums, arrange them such that they form the largest number and return it.

Since the result may be very large, so you need to return a string instead of an integer.

## EX

Example 1:

Input: nums = [10,2]
Output: "210"


Example 2:

Input: nums = [3,30,34,5,9]
Output: "9534330"


## Constraints:

1 <= nums.length <= 100

0 <= nums[i] <= 10^9

# 思路

String s1 = "9";
String s2 = "31";

String case1 =  s1 + s2; // 931
String case2 = s2 + s1; // 319


# java 实现

class Solution {

public String largestNumber(int[] nums) {
//1. int 转 string
List<String> stringList = new ArrayList<>(nums.length);
for(int i : nums) {
}

//2. 排序
Collections.sort(stringList, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
String s1 = o1+o2;
String s2 = o2+o1;

// reverse order here, so we can do append() later
return s2.compareTo(s1);
}
});

//3. 构建结果
// An extreme edge case by lc, say you have only a bunch of 0 in your int array
if(stringList.get(0).charAt(0) == '0') {
return "0";
}

StringBuilder sb = new StringBuilder();
for(String s: stringList) {
sb.append(s);
}

return sb.toString();
}

}


# 数学证明

## 定理

We use a.b to represent the concatenation of non-negative integers a and b .

Theorem:

Let a, b, and c be non-negative integers.

If a.b > b.a and b.c > c.b , we have a.c > c.a .

## 证明

Proof:

We use [a] to denote the length of the decimal representation of a .

For example, if a = 10 , we have [a] = 2 .

Since a.b > b.a and b.c > c.b , we have

a * 10^[b] + b > b * 10^[a] + a
b * 10^[c] + c > c * 10^[b] + b


, which is equivalent to

a * (10^[b] - 1) > b * (10^[a] - 1)
b * (10^[c] - 1) > c * (10^[b] - 1)


Obviously, 10^[a] - 1 > 0 , 10^[b] - 1 > 0 , and 10^[c] - 1 > 0 .

Since c >= 0 , according to the above inequalities, we know that b > 0 and a > 0 .

After multiplying the above two inequalities and cancelling b and (10^[b] - 1) , we have

a * (10^[c] - 1) > c * (10^[a] - 1)

This is equivalent to

a * 10^[c] + c > c * 10^[a] + a

, which means a.c > c.a .

Q.E.D.

# 参考资料

https://leetcode.com/problems/largest-number/

https://leetcode.com/problems/largest-number/discussion/