LeetCode 179 Largest Number 把数组排成最大的数

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.

 class Solution {
public:
string largestNumber(vector<int>& nums) {
vector<string> arr;
for(int n:nums)
arr.push_back(to_string(n));
sort(arr.begin(),arr.end(),[](string &str1,string &str2){return str1+str2>str2+str1;});
string res;
for(string s:arr)
res+=s;
if(res[]=='')
return "";
return res;
}
};
上一篇:[LeetCode] 179. Largest Number 解题思路


下一篇:Leetcode 179 Largest Number 贪心