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.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
AC代码,非常优雅:
class Solution {
public:
string largestNumber(vector<int>& nums) {
string res;
sort(nums.begin(),nums.end(),[](int a,int b){
return to_string(a)+to_string(b)>to_string(b)+to_string(a);
});
for(auto num:nums)
res+=to_string(num);
return res[0]=='0'?"0":res;
}
};
本文介绍了一种优雅的方法来解决将整数数组重新排列形成最大可能数值的问题。通过自定义排序算法,确保数字组合能构成最大的可能数值,并提供了一个简洁的C++实现方案。
3283

被折叠的 条评论
为什么被折叠?



