剑指offer 面试题45. 把数组排成最小的数 【自定义排序】

面试题45. 把数组排成最小的数(自定义排序,清晰图解)

https://leetcode-cn.com/problems/ba-shu-zu-pai-cheng-zui-xiao-de-shu-lcof/solution/ba-shu-zu-pai-cheng-zui-xiao-de-shu-jian-dan-yi-do/

class Solution {
public:
    string minNumber(vector<int>& nums) {
        // 选取使字符串组合更小的排序规则
        auto compare = [](string sa, string sb){return sa+sb < sb+sa;};
        vector<string> tmp;
        for(int n : nums){
            tmp.push_back(to_string(n));
        }
        sort(tmp.begin(), tmp.end(), compare);
        string ans = "";
        for(string s : tmp) ans += s;
        return ans;
    }
};

猜你喜欢

转载自blog.csdn.net/g534441921/article/details/105538565