剑指offer-把数组排成最小的数

题目描述

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

代码如下

#include "JzOffer.h"
string Int2Str(int num);
//定义结构
struct StrNum
{
	string strnum;
	StrNum(const string& _s) : strnum(_s) {}
	bool operator<(const StrNum& _s2) const
		{
		int i = 0, j = 0;
		char c1 = this -> strnum[0];
		char c2 = _s2.strnum[0];
		while(true)
			{
			if(i == this->strnum.length()) i--;
			else c1 = this->strnum[i];

			if(j == _s2.strnum.length()) j--;
			else c2 = _s2.strnum[j];
			if(c1 != c2)
			{
				break;
			}
				else
			{
				i++;
				j++;
			}
		}
		return c1 < c2;
	}
};
string PrintMinNumber(vector<int> numbers)
	{
		string outstr;
		if(numbers.empty()) return outstr;
		std::map<StrNum,int> strnums;
		std::map<StrNum,int>::iterator imap;
		for(int i = 0; i < numbers.size(); i++)
			{
				string instr = Int2Str(numbers[i]);
				imap = strnums.find(instr);
				if (imap == strnums.end())
				{
					strnums.insert(make_pair(instr,1));
				}
				else
				{
					imap->second++;
				}
			}
		imap = strnums.begin();
		while (imap != strnums.end())
		{
			while(imap->second)
				{ 
					outstr += imap->first.strnum;
					imap->second--;
				}
			imap++;
		}
		return outstr;
	}
bool TwoStr2Compare(const string& str1, const string& str2)
	{
	int i = 0, j = 0;
	char c1 = str1[0];
	char c2 = str2[0];
	while(true)
		{
		if(i == str1.length()) i--;
		else c1 = str1[i];

		if(j == str2.length()) j--;
		else c2 = str2[j];
		if(c1 != c2)
			{
			break;
			}
		else
			{
			i++;
			j++;
			}
		}
	return c1 < c2;
	}
string Int2Str(int num)
	{
	string outstr;
	while(num)
		{
			outstr.push_back(num%10 + 48);
			num /= 10;
		}
	int sizeOfStr = outstr.size();
	for(int i = 0, j = sizeOfStr - 1; i <= sizeOfStr / 2, j >= sizeOfStr / 2; i++, j--)
		{
			char temp = outstr[i];
			outstr[i] = outstr[j];
			outstr[j] = temp;
		}
	return outstr;
	}
void PrintMinNumberTest()
	{
		int Array[3] = {3,32,321};
		vector<int> vecint;
		for(int i = 0; i < 3; i++)
			vecint.push_back(Array[i]);
		vector<string> strnums;
		for(int i = 0; i < 3; i++)
			strnums.push_back(Int2Str(vecint[i]));
		sort(strnums.begin(),strnums.end(),TwoStr2Compare);
		PrintMinNumber(vecint);
	}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值