LintCode-最大数

分享代码~

bool cmp(string a,string b)

{
    if (a+b>b+a)
        return true;
    else
        return false;
}


class Solution {
public:
    /**
     *@param num: A list of non negative integers
     *@return: A string
     */
     string toString(int x)
     {
         if (x==0)
         {
             return "0";
         }
         string s="";
         while (x>0)
         {
             char ch='0'+x%10;
             s=ch+s;
             x/=10;
         }
         return s;
     }
    string largestNumber(vector &num) {
        // write your code here
        vector nums;
        for (int i=0;i         {
            nums.push_back(toString(num[i]));
        }
        sort(nums.begin(),nums.end(),cmp);
        string r="";
        for (int i=0;i         {
            r+=nums[i];
        }
        if (r[0]=='0')
            return "0";
        return r;
    }
};

你可能感兴趣的:(LintCode-最大数)