把数组排成最小的数(数字转化为字符串排序)
把数组排成最小的数
http://www.nowcoder.com/questionTerminal/8fecd3f8ba334add803bf2a06af1b993
/*
to_string:int double 等类型转化为string
根据两个数顺序的大小进行排序
*/
class Solution {
public:
string PrintMinNumber(vector<int> num) {
string ans;
if(!num.size())return ans;
sort(num.begin(), num.end(), cmp);
for(auto x:num){
ans+=to_string(x);
}
return ans;
}
static bool cmp(int a, int b){
return to_string(a)+ to_string(b) < to_string(b) + to_string(a);
}
};
查看14道真题和解析
阿里云工作强度 600人发布