您的位置:首页 > 其它

[LeetCode] Largest Number

2015-06-09 17:13 381 查看
Well, this problem is designed for radix sort. For more information about radix sort, Introduction to Algorithms, 3rd edition has some nice examples.

However, it can be solved simply by using the sort function while defining a new comparison function for it.

The code is pretty straight-forward.

static bool cmp(int s, int t) {
return to_string(s) + to_string(t) > to_string(t) + to_string(s);
}
string largestNumber(vector<int>& nums) {
sort(nums.begin(), nums.end(), cmp);
string ans;
for (int i = 0; i < (int)nums.size(); i++)
ans += to_string(nums[i]);
if (ans[0] == '0') return "0";
return ans;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: