简单的采用sort进行求值。用到了string的C++11的功能 to_string. 以及自定义的sort的cmp函数来进行求职
/**
* @author johnsondu
* @problem Largest Number
* @url https://leetcode.com/problems/largest-number/
* @timeComlexity O(n)
* @spaceComplexity O(n)
* @strategy using sort to compare
*/
#include <algorithm>
class Solution {
public:
static bool cmp(const string &a, const string &b) {
return (a + b) > (b + a);
}
string largestNumber(vector<int>& nums) {
int n = nums.size();
vector<string> vec;
for(int i = 0; i < n; i ++) {
vec.push_back(to_string(nums[i]));
}
sort(vec.begin(), vec.end(), cmp);
string ans("");
for(int i = 0; i < n; i ++) {
ans += vec[i];
}
return (ans[0] == '0' ? "0" : ans);
}
};