剑指offer - 面试题33 - 把数组排成最小的数

#include<iostream>
#include<vector>
#include<string>
#include<strstream>
#include<algorithm>
bool compare(const string & num1, const string & num2)
{
    string str1 = num1 + num2;
    string str2 = num2 + num1;
    return str1<str2;
}
class Solution {
public:

string PrintMinNumber(vector<int> numbers)
{
    string res = "";
    int len = numbers.size();
    if (len>0) 
    {
        vector<string> strNumbers(len);
        for (int i = 0;i < len;i++)
        {
            strstream ss;
            ss << numbers[i];
            ss >> strNumbers[i];
        }
        sort(strNumbers.begin(), strNumbers.end(), compare);
        for (auto e : strNumbers)
            res += e;
    }
    return res;
}
};

留意C++中sort函数的用法,C中qsort的函数用法参见:http://www.cnblogs.com/CCBB/archive/2010/01/15/1648827.html

posted @ 2017-10-05 20:30  蓦然闻声  阅读(101)  评论(0编辑  收藏  举报