class Solution {
public:
string largestNumber(vector<int>& nums) {
auto cmp = [](const int &x, const int &y) {
long sx = 10, sy = 10;
while (sx <= x) {
sx *= 10;
}
while (sy <= y) {
sy *= 10;
}
return sy * x + y > sx * y + x;//如果为真就是第一个参数在前第二个参数在后
};
sort(nums.begin(), nums.end(),cmp);
string result;
if (nums[0] == 0) {//已经按照从大到小排序了
return "0";
}
for (int &x : nums) {
result += to_string(x);
}
return result;
}
};
stringstream
// int -> string
stringstream sstream;
string strResult;
int nValue = 1000;
// 将int类型的值放入输入流中
sstream << nValue;
// 从sstream中抽取前面插入的int类型的值,赋给string类型
sstream >> strResult;
stringstream sstream;
// 将多个字符串放入 sstream 中
sstream << "first" << " " << "string,";
sstream << " second string";
// 可以使用 str() 方法,将 stringstream 类型转换为 string 类型;
cout << "strResult is: " << sstream.str() << endl;
// 清空 sstream 之 str("")
sstream.str("");
// 清空 sstream 之 clear() 方法
sstream.clear();
class Solution {
public:
string largestNumber(vector<int>& nums) {
vector<string> str;
for(auto i : nums) {
str.push_back(to_string(i));
}
// 直接根据拼接结果
auto cmp = [](string left, string right) {
return left + right > right + left;
};
sort(str.begin(),str.end(), cmp);
stringstream ss;
for(auto c : str) {
ss << c;
}
string ans = ss.str();
if(ans[0] == '0'){
return "0";
}
return ans;
}
};