C++ STL solution


  • 0
    D

    Nothing fancy.

    class Solution {
       public:
        string largestNumber(vector<int>& nums) {
            if (nums.empty()) return "";
            sort(nums.begin(), nums.end(), [](const int& a, const int& b) {
                return to_string(a) + to_string(b) > to_string(b) + to_string(a);
            });
            auto res = accumulate(
                nums.begin(), nums.end(), string(),
                [](const string& a, const int& b) { return a + to_string(b); });
            return res[0] == '0' ? "0" : res;
        }
    };
    

Log in to reply
 

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