Largest Number

Description:

Given a list of non negative integers, arrange them such that they form the largest number.
For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.

Note: The result may be very large, so you need to return a string instead of an integer.

Solution:

class Solution {
public:
string largestNumber(vector& nums) {
vector strs;
for (auto& n : nums) strs.push_back(to_string(n));
sort(begin(strs), end(strs), [](string& a, string& b) { return a+b>b+a; });
string rc;
for (auto& s : strs) rc += s;
int i = 0;
for (; i < (int)rc.length(); ++i) if (rc[i] != '0') break;
return i == rc.length() ? "0" : rc.substr(i);
}
};

posted @ 2015-08-18 00:10  影湛  阅读(146)  评论(0)    收藏  举报