码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] Largest Number

时间:2015-06-09 19:48:48      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

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.

 1     static bool cmp(int s, int t) {
 2         return to_string(s) + to_string(t) > to_string(t) + to_string(s);
 3     }
 4     string largestNumber(vector<int>& nums) {
 5         sort(nums.begin(), nums.end(), cmp);
 6         string ans;
 7         for (int i = 0; i < (int)nums.size(); i++)
 8             ans += to_string(nums[i]);
 9         if (ans[0] == 0) return "0";
10         return ans;
11     }

[LeetCode] Largest Number

标签:

原文地址:http://www.cnblogs.com/jcliBlogger/p/4564052.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!