179. Largest Number
problem description
Input: [10,2]
Output: "210"Input: [3,30,34,5,9]
Output: "9534330"algorithm thought
code
class Solution {
public:
string largestNumber(vector<int>& nums) {
vector<string> tmp;
for(int num:nums)
tmp.push_back(to_string(num));
sort(tmp.begin(),tmp.end(),[](string s1,string s2){return s1+s2>s2+s1;});
string res;
for(string str:tmp)
res+=str;
while(res[0]=='0'&&res.size()>1){
res.erase(0,1);
}
return res;
}
};algorithm analysis
Last updated