Problem description:

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

Example 1:

1
2
Input: [10,2]
Output: "210"

Example 2:

1
2
Input: [3,30,34,5,9]
Output: "9534330"

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

Solution:

First, sort the array based on the combination of two integer.
After sorting, we can use the array to create result string.
For example:
Input: [10,2]
“2”+”10”-> 210
“10”+”2”-> 102

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
string largestNumber(vector<int>& nums) {
sort(nums.begin(), nums.end(), [](int a, int b){ return to_string(a)+to_string(b)> to_string(b)+to_string(a);});
if (nums[0] == 0) return "0";
string res = "";
for (auto num : nums) {
res = res + to_string(num);
}
return res;
}
};