题解 | #最大数#

最大数

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

排序题

class Solution {
public:
    /**
     * 最大数
     * @param nums int整型vector 
     * @return string字符串
     */
    string solve(vector<int>& nums) {
        // write code here
        sort(nums.begin(), nums.end(), [](int a, int b){
            return to_string(a) + to_string(b) > to_string(b) + to_string(a);
        });
        string str;
        for(int x:nums){
            str += to_string(x);
        }
        while(str.length() > 1 && str[0] == '0') str.erase(0, 1); //去掉前导0
        return str;
    }
};
全部评论

相关推荐

咖啡不掺水:团子一志愿嵌入式今日已挂,等捞😭
投递美团等公司6个岗位 美团求职进展汇总
点赞 评论 收藏
分享
评论
3
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务