Given a list of non negative integers, arrange them such that they form the largest number.
Example 1:
Input:[10,2]
Output: "210"
Example 2:
Input:[3,30,34,5,9]
Output: "9534330"
Note: The result may be very large, so you need to return a string instead of an integer.
Related Topics:
Sort
// OJ: https://leetcode.com/problems/largest-number/
// Author: github.com/lzl124631x
// Time: O(NlogN * W) where W is the max length of the number strings.
// Space: O(NW)
class Solution {
public:
string largestNumber(vector<int>& A) {
vector<string> v;
for (int n : A) v.push_back(to_string(n));
sort(begin(v), end(v), [](auto &a, auto &b) {
for (int i = 0; i < a.size() + b.size(); ++i) {
char x = i < a.size() ? a[i] : b[i - a.size()], y = i < b.size() ? b[i] : a[i - b.size()];
if (x != y) return x > y;
}
return false; // don't return true which will result in infinite loop
});
if (v[0] == "0") return "0";
string ans;
for (auto &s : v) ans += s;
return ans;
}
};
Or
// OJ: https://leetcode.com/problems/largest-number/
// Author: github.com/lzl124631x
// Time: O(NlogN * W) where W is the max length of the number strings.
// Space: O(NW)
class Solution {
public:
string largestNumber(vector<int>& A) {
vector<string> v;
for (int n : A) v.push_back(to_string(n));
sort(begin(v), end(v), [](auto &a, auto &b) { return a + b > b + a; });
if (v[0] == "0") return "0";
string ans;
for (auto &s : v) ans += s;
return ans;
}
};