class Solution {
public:
string largestNumber(vector<int> &num) {
int n = num.size();
vector<string> numstrs(n);
for(int i = 0; i < n; i++)
{
numstrs[i] = to_string(num[i]); // to_string() is standard lib func
}
sort(numstrs.begin(), numstrs.end(), myCompare);
if (numstrs[n-1] == "0")
{
return "0";
}
string res;
for(int i = n-1; i >= 0; i--)
{
res += numstrs[i];
}
return res;
}
static bool myCompare (string str1, string str2)
{
return (str1 + str2) < (str2 + str1);
}
};
No comments:
Post a Comment