1. 程式人生 > >劍指offer - 面試題33 - 把數組排成最小的數

劍指offer - 面試題33 - 把數組排成最小的數

print offer include .html str 把數組排成最小的數 pri tr1 class

#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 - 把數組排成最小的數