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;
}
};

本文介绍了一种算法,该算法接收一个非负整数列表,并通过重新排列这些整数形成可能的最大数值。具体实现中使用了自定义字符串比较方法来确定最佳排序顺序。
784

被折叠的 条评论
为什么被折叠?



