Largest Number


Given a list of non negative integers, arrange them such that they form the largest number.

For example, given ​​[3, 30, 34, 5, 9]​​, the largest formed number is ​​9534330​​.

Note: The result may be very large, so you need to return a string instead of an integer.

Credits:
Special thanks to ​​​@ts​​ for adding this problem and creating all test cases.

class Solution {
public:
//cmp 要声明为静态函数
static bool cmp(int a,int b)
{
char str1[15],str2[15];
sprintf(str1,"%d",a);
sprintf(str2,"%d",b);
string aa,bb;
aa+=str1;aa+=str2;
bb+=str2;bb+=str1;
return aa>bb;
}

string largestNumber(vector<int>& nums) {

string ans;
int n=nums.size();
int i;

sort(nums.begin(),nums.end(),cmp);
for(i=0;i<n;i++)
{
char str[15];
sprintf(str,"%d",nums[i]);
ans+=str;
}
i=0;
while(ans[i]=='0'&&i<n)
i++;
if(i==n)
return "0";
return ans.substr(i);
}


};