My O(nlogn) c++ solution


  • 4
    Q
    class Solution {
    public:
        string largestNumber(vector<int> &num) 
        {
            vector<string> snum;
            for (auto i : num)
                snum.push_back(to_string(i));
            
            sort(snum.begin(), snum.end(), 
                [](string a, string b) 
                {
                    return a + b > b + a;
                }
            );
            
            string res = "";
            for (auto s : snum)
                res += s;
            if (res == "" || res[0] == '0') return "0";
            return res;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.