首页 > 其他 > 详细

[LeetCode] Largest Number

时间:2015-06-09 19:46:02      阅读:152      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!