Skip to content

Latest commit

 

History

History
24 lines (18 loc) · 636 Bytes

33.md

File metadata and controls

24 lines (18 loc) · 636 Bytes

剑指Offer之面试题33:把数组排成最小的数

输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如,输入数组{3,32,321},则打印出这三个数字能排成的最小数字321323。

1 实现

struct comp {
	int operator()(string num1, string num2)
	{
		string str1 = num1 + num2;
		string str2 = num2 + num1;

		return str1.compare(str2);
	}
};

void min_num(vector<string>::iterator beg, vector<string>::iterator end)
{
	sort(beg, end, comp());

	copy(beg, end, ostream_iterator<string>(cout, ""));
}