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

 

posted on 2015-03-20 06:09  keepshuatishuati  阅读(125)  评论(0编辑  收藏  举报