题解 | #最大数#

最大数的求法核心还是在于sort函数的编写

public:
        bool cmp(string a,string b){
        return a + b > b + a;
        }
        
    string solve(vector<int>& nums) {
        // write code here
        string str;
        sort(nums.begin(),nums.end(),[](int const& x,int const& y){//方法一:lambda 匿名函数 []表示没有外部变量传入,[=]表示值传递,[&]表示引用传递
            int dx=10,dy=10;
            while(dx<=x)
            {
                dx*=10;
            }
            while(dy<=y)
            {
                dy*=10;
            }
            return x*dy+y>y*dx+x;
        });
        
        //for(int i  = 0;i < nums.size();i ++){
        //    ve.push_back(to_string(nums[i]));
        //}
        //sort(ve.begin(),ve.end(),cmp);//方法二
  
        if(nums[0]==0)
        {
            return "0";
        }
        for(auto num:nums)
        {
            str+=to_string(num);
        }
        return str;
    }

};
Leetcode刷题整合 文章被收录于专栏

都是作者刷到的一些感觉是好题整理到一起的,辛苦整理不易,麻烦给个赞,有疑问请留言

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务