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.
注意sort的使用,string大小比较,compare定义
class Solution {
public:
string largestNumber(vector<int>& nums) {
vector<string> num_str;
for (int i = 0; i < nums.size(); i++) {
num_str.push_back(nums[i]);
}
sort(num_str.begin(), num_str.end(), compare);
string res = "";
for (int i = 0; i < num_)
}
private:
static bool compare(string num1, string num2) {
return num1 + num2 > num2 + num1;
}
};