Leetcode 179 Solution

This article provides solution to leetcode question 179 (largest-number)

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;
    }
};