题目:
class Solution {
public:
string minNumber(vector<int>& nums) { //这道题要学会重构字符串的比较排序
vector<string> str; //将数组全部转化为字符串进行比较
string result;
for(int i=0;i<nums.size();i++){
str.push_back(to_string(nums[i]));
}
sort(str.begin(),str.end(),[] (string& s1, string& s2) {return s1+s2<s2+s1;}); //这里用到lamda表达式。因为10+1的顺序优于1+10,所以重构函数为s1+s2<s2+s1(要有return和;)
for(auto s:str){
result+=s;
}
return result;
}
};
作者:Philos
链接:https://leetcode.cn/problems/ba-shu-zu-pai-cheng-zui-xiao-de-shu-lcof/solutions/247180/c-xian-zhuan-huan-cheng-zi-fu-chuan-zai-zu-he-by-y/
来源:力扣(LeetCode)