面试题45:把数组排成最小的数
文章目录
题目
输入一个非负整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。
考点
to_string
sort
lambda表达式
append
LeetCode版本
class Solution {
public:
string minNumber(vector<int>& nums) {
string res;
if(nums.empty()) return res;
vector<string> str;
for(auto num:nums){
str.push_back(to_string(num));
}
sort(str.begin(), str.end(), [](string x, string y){ return x+y < y+x;});
for(auto s:str){
res.append(s);
}
return res;
}
};