Last updated 3 years ago
Sort the input such that simply appending them one after another forms the result
Sort using special comparator function that checks for both possibilities of positions for any two given numbers.
bool comp(string n1, string n2) { return n1 + n2 > n2 + n1; } string Solution::largestNumber(const vector<int> &A) { string result; vector<string> str; bool allZero = true; for(int x: A) { if(allZero && x) allZero = false; str.push_back(to_string(x)); } if(allZero) return "0"; sort(str.begin(), str.end(), comp); for(string x: str) result += x; return result; }