179. Largest Number

来源:互联网 发布:oracle 数据块大小 编辑:程序博客网 时间:2024/06/05 16:08

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;    }};

原创粉丝点击