Largest Number

Desicription

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
string largestNumber(vector<int>& nums) {
vector<string> s;
for(auto it : nums)
s.push_back(to_string(it));
sort(s.begin(), s.end(), [](string a, string b){return a + b > b + a;});
string res;
for(auto it : s)
res += it;
while(res[0] == '0' && res.size() > 1)
res.erase(0, 1);
return res;
}
};