Leetcode 179 Solution
This article provides solution to leetcode question 179 (largest-number)
Access this page by simply typing in "lcs 179" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/largest-number
Solution
class Solution {
public:
string largestNumber(vector<int>& nums) {
vector<string> strs;
for (auto it = nums.begin(); it != nums.end(); it++)
strs.push_back(to_string(*it));
sort(strs.begin(), strs.end(),
[] (const string& s1, const string& s2)
{
string t1 = s1 + s2;
string t2 = s2 + s1;
return t1 > t2;
});
string res;
for (auto it = strs.begin(); it != strs.end(); it++)
res += *it;
int i = 0;
while (i < res.size() && res[i] == '0')
i++;
return i == res.size() ? "0" : res;
}
};