给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数。
示例 1:
输入: [10,2] 输出: 210
示例 2:
输入: [3,30,34,5,9] 输出: 9534330
说明: 输出结果可能非常大,所以你需要返回一个字符串而不是整数。
注意全是0的情况
bool cmp3(int x, int y)
{
string str1 = to_string(x);
string str2 = to_string(y);
return (str1 + str2) > (str2 + str1);
}
class Solution {
public:
string largestNumber(vector<int>& nums)
{
if(nums.size() == 0)
return "0";
sort(nums.begin(), nums.end(), cmp3);
string str = "";
int x = 0;
for(int i = 0; i < nums.size(); i++)
{
x += nums[i];
str = str + to_string(nums[i]);
}
if(x == 0)
return "0";
return str;
}
};