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

LeetCode – Refresh – Largest Number

时间:2015-03-20 06:58:14      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:

Corner case: when all the elements are 0. It should return "0", not  "00000000".

It use string to compare all the numbers.

 1 class Solution {
 2 public:
 3     string largestNumber(vector<int> &num) {
 4         int len = num.size();
 5         if (len == 0) return 0;
 6         vector<string> container;
 7         for (int i = 0; i < len; i++) {
 8             container.push_back(to_string(num[i]));
 9         }
10         sort(container.begin(), container.end(), [](string &a, string &b){return a+b > b+a;});
11         string result;
12         for (int i = 0; i < len; i++) {
13             result += container[i];
14         }
15         if (result[0] == 0) return "0";
16         return result;
17     }
18 };

 

LeetCode – Refresh – Largest Number

标签:

原文地址:http://www.cnblogs.com/shuashuashua/p/4352640.html

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