字符串排序

58. 把数组排成最小的数

输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。

例如输入数组[3, 32, 321],则打印出这3个数字能排成的最小数字321323。

class Solution {
public:
    string printMinNumber(vector<int>& nums) {
        vector<string> temp;
        for(auto x:nums) temp.push_back(to_string(x));
        sort(temp.begin(),temp.end(),[](const string&a, const string&b){
            return a+b < b+a;//注意
        });
        string s;
        for(auto x:temp) s += x;
        return s;
    }
};

 

posted @ 2020-02-26 21:10  swiftAlien  阅读(245)  评论(0编辑  收藏  举报