class Solution {
public:
string largestNumber(vector<int>& nums) {
std::sort(nums.begin(), nums.end(), [](int a, int b)
{
int a1 = a;
int n = 1;
long a2 = 10;
while (a1 >= 10)
{
a1 /= 10;
a2 *= 10;
n++;
}
int b1 = b;
int m = 1;
long b2 = 10;
while (b1 >= 10)
{
b1 /= 10;
b2 *= 10;
m++;
}
return a*b2+b>b*a2+a;
});
if (nums[0] == 0)
{
return "0";
}
string ret = "";
for (int i = 0; i < nums.size(); i++)
{
char buff[100];
sprintf(buff, "%d", nums[i]);
ret += buff;
}
return ret;
}
};
public:
string largestNumber(vector<int>& nums) {
std::sort(nums.begin(), nums.end(), [](int a, int b)
{
int a1 = a;
int n = 1;
long a2 = 10;
while (a1 >= 10)
{
a1 /= 10;
a2 *= 10;
n++;
}
int b1 = b;
int m = 1;
long b2 = 10;
while (b1 >= 10)
{
b1 /= 10;
b2 *= 10;
m++;
}
return a*b2+b>b*a2+a;
});
if (nums[0] == 0)
{
return "0";
}
string ret = "";
for (int i = 0; i < nums.size(); i++)
{
char buff[100];
sprintf(buff, "%d", nums[i]);
ret += buff;
}
return ret;
}
};
评论
发表评论