LeetCode179(2021.4.12)

LC179. 最大数

class Solution {
public:
    static bool cmp(string a, string b) {
        return a + b > b + a;
    }

    string largestNumber(vector<int> &nums) {
        vector<string> numstr;
        for (int i:nums) numstr.push_back(to_string(i));
        sort(numstr.begin(), numstr.end(), cmp);
        if (numstr[0] == "0") return numstr[0];
        string res = "";
        for (string i:numstr) res += i;
        return res;
    }
};

发表评论