Largest Number
Desicription
Given a list of non negative integers, arrange them such that they form the largest number.
Example 1:
代码语言:javascript复制Input: [10,2]
Output: "210"
Example 2:
代码语言:javascript复制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
代码语言:javascript复制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;
}
};