struct compare {
bool operator()(const string s1, const string s2) {
return ((s1+s2) > (s2+s1));
}
};
class Solution {
public:
string largestNumber(vector<int>& nums) {
string res;
vector<string> ns;
for(auto s:nums)
ns.push_back(to_string(s));
sort(ns.begin(),ns.end(),compare());
if (ns[0] == "0")
return "0";
for(auto s:ns)
res+=s;
return res;
}
};
比较器
大于比较器
struct compare {
bool operator()(const string s1, const string s2) {
return ((s1+s2) > (s2+s1));
}
};
//使用
sort(ns.begin(),ns.end(),compare());
static bool comparetor(string s1, string s2) {
return (s1+s2) > (s2+s1);
}
//使用
sort(ns.begin(), ns.end(), comparetor);
小于比较器
struct compare {
bool operator()(const string s1, const string s2) {
return ((s1+s2) < (s2+s1));
}
};
//使用
sort(ns.begin(),ns.end(),compare());
static bool comparetor(string s1, string s2) {
return (s1+s2) < (s2+s1);
}
//使用
sort(ns.begin(), ns.end(), comparetor);