题解 | #把数组排成最小的数#
把数组排成最小的数
http://www.nowcoder.com/practice/8fecd3f8ba334add803bf2a06af1b993
class Solution {
public:
string PrintMinNumber(vector<int> numbers) {
string ans;
if(numbers.empty()) return ans;
vector<string> strs;
for(auto i :numbers)
{
strs.push_back(to_string(i));
}
sort(strs.begin(),strs.end(),compare);
for(auto i:strs)
ans.append(i);
return ans;
}
static int compare(string &a,string &b)
{
string ab=a+b;
string ba=b+a;
return ab<ba;
}
};
public:
string PrintMinNumber(vector<int> numbers) {
string ans;
if(numbers.empty()) return ans;
vector<string> strs;
for(auto i :numbers)
{
strs.push_back(to_string(i));
}
sort(strs.begin(),strs.end(),compare);
for(auto i:strs)
ans.append(i);
return ans;
}
static int compare(string &a,string &b)
{
string ab=a+b;
string ba=b+a;
return ab<ba;
}
};