LeetCode—Largest Number

Given a list of non negative integers, arrange them such that they form the largest number.

For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.

Note: The result may be very large, so you need to return a string instead of an integer.

注意返回的是一个字符串

在这个例子中感觉最难想到的是两个数字之间的比较,这里并不是数值上的比较,而是从最高位开始进行的比较

用一个简单的比较函数就可以得到结果

 bool cmp(string str1,string str2)
{
return (str1+str2) >= (str2+str1);
}

下面是是我的解答方法:(注意如果出现多个0的情况那么直接作为一个0)

<span style="color:#333333;">#include <sstream> 
#include <string>
#include <vector>
using namespace std;

bool cmp(string str1,string str2)
{
	return (str1+str2) >= (str2+str1);
}
string GetSort(vector<string> vecString)
{
	int length = vecString.size();
	string maxStr = "";
	bool flag = false;
	while (!vecString.empty())
	{
		string maxVal = "0";
		vector<string>::iterator maxRecode;
		vector<string> :: iterator it = vecString.begin();
		for (;it < vecString.end(); it++)
		{
			if (cmp(*it,maxVal))
			{
				maxVal = *it;
				maxRecode = it;
			}
		}
		vecString.erase(maxRecode);
		if (maxVal != "0")
		{
			maxStr += maxVal;
			flag = true;
		}
		else if(flag)
		{
			maxStr += maxVal;
		}
     }
	if (!flag)
	{
		maxStr += "0";
	}
	return maxStr;
}
string largestNumber(vector<int> &num) {
	vector<string> vecString;
	string maxValNum = "";
	int length = num.size();
	if(length == 0)
	{
		return NULL;
	}
	if(length == 1)
	{
		stringstream ss;
</span><span style="color:#ff0000;">		ss << num[0];
		return ss.str();  //<将int转化为string</span><span style="color:#333333;">
	}	
	for (int i = 0; i < num.size(); i++)
	{
		stringstream ss;
		ss << num[i];
		vecString.push_back(ss.str());
	}
	//stack<int> stackStr = GetSort(vecString);
	maxValNum = GetSort(vecString);
	return maxValNum;
}	
	

void main()
{
	vector<int> it;
	it.push_back(13);
	it.push_back(9);
	it.push_back(0);
	string x = largestNumber(it);
}</span>
其中也有方法直接利用STL对应的sort函数,进行相应的排序

<span style="color:#333333;">#include <sstream> 
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

bool cmp(const string s1, const string s2) {
	return (s1 + s2) > (s2 + s1);
}

string largestNumber(vector<int> &num) {
	vector<string> s_num(num.size());
	stringstream stream;
	for (int i = 0; i < num.size(); ++i) {
		stream << num[i];
		stream >> s_num[i];
		stream.clear();
	}
	</span><span style="color:#ff0000;"><strong>sort(s_num.begin(), s_num.end(), cmp);</strong></span><span style="color:#333333;">
	string tmp_res;
	for (int i = 0; i < s_num.size(); ++i) {
		tmp_res += s_num[i];
	}
	string res;
	bool flag = false;
	for (int i = 0; i < tmp_res.size(); ++i) {
		if (tmp_res[i] != '0') {
			res.push_back(tmp_res[i]);
			flag = true;
		} else if (flag) {
			res.push_back(tmp_res[i]);
		}
	}
	if (!flag) res.push_back('0');
	return res;
}	
	

void main()
{
	vector<int> it;
	it.push_back(13);
	it.push_back(9);
	it.push_back(0);
	string x = largestNumber(it);
}</span>



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值