解题思路
1.排序
2.不相同数字开头的,依次比较高位
3.相同数字开头的,比较两个数不同排列后的大小
代码
class Solution {
public:
string largestNumber(vector<int>& nums) {
//自己定义的排序 sy * x + y > sx * y + x
sort(nums.begin(),nums.end(),[](const int &x,const int &y){
long sx = 10, sy = 10;
while(x >= sx){
sx *= 10;
}
while(y >= sy){
sy *= 10;
}
return sy * x + y > sx * y + x;
});
//遍历成字符串拼接
string str;
for(auto num:nums){
if(!(num == 0 && str[0] == '0'))
str += to_string(num);
}
return str;
}
};