码迷,mamicode.com
首页 > 其他好文 > 详细

179. Largest Number -- 数字字符串比较大小

时间:2016-08-23 21:52:53      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:

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.

class Solution {
    static bool cmp(string s1, string s2) //字符串比较 GOOD!
    {
        return s1+s2 > s2+s1;
    }
    
public:
    string largestNumber(vector<int>& nums) {
        vector<string> vs;
        for(int i = 0; i < nums.size(); i++)
            vs.push_back(to_string(nums[i])); //int转string
        sort(vs.begin(), vs.end(), cmp);
        string s = "";
        for(int i = 0; i < vs.size(); i++)
            s += vs[i];
        if(s[0] == 0)
            return "0";
        return s;
    }
};

 

179. Largest Number -- 数字字符串比较大小

标签:

原文地址:http://www.cnblogs.com/argenbarbie/p/5800805.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!