首页 > 编程语言 > 详细

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

时间:2017-10-05 20:43:50      阅读:282      评论:0      收藏:0      [点我收藏+]
#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

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

原文:http://www.cnblogs.com/vincent93/p/7629924.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!