题解 | #最大数#

最大数

http://www.nowcoder.com/practice/fc897457408f4bbe9d3f87588f497729

将数字转成字符串,然后对字符串按指定规则排序

指定规则:a+b>b+a,则a排在b前面

class Solution {
public:
    static bool cmp(string a, string b){
        return a+b > b+a;
    }
    string solve(vector<int>& nums) {
        // write code here
        vector<string> s;
        for(int i=0; i<nums.size(); ++i){
            s.push_back(to_string(nums[i]));
        }
        sort(s.begin(), s.end(), cmp);
        if(s[0] == "0") return "0";
        string res = "";
        for(int i=0; i<nums.size(); ++i)
            res += s[i];
        return res;
    }
};
全部评论

相关推荐

野猪不是猪🐗:是我导致的,我前天对力扣进行了跨站脚本攻击,网站把我的请求给block了(胡言乱语)
点赞 评论 收藏
分享
lingo12:1.最好加个业务项目,大部分面试官工作以后会更偏重业务 2.实习部分描述一般般,可能hr看到会觉得你产出不够不给你过简历 3.蓝桥杯这些大部分人都有的,不如不写,反而减分项。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务