您的位置:首页 > 其它

leetcode 179. Largest Number

2016-03-24 17:05 489 查看
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.

仓促写完,代码写得太凌乱了

class Solution {
void do_once(vector<string>& nums,vector<pair<string, map<int, vector<int>, greater<int>>>>&candi)
{
vector<pair<string, map<int, vector<int>, greater<int>>>>newcandi;
int minlen = 1000000000;
for (int i = 0;i<candi.size();i++)
{
if (candi[i].second.begin()->second.size() == 1)
{
map<int, vector<int>, greater<int>>count = candi[i].second;
string str =nums[ count.begin()->second[0]];
count.erase(count.begin());
if (find(newcandi.begin(), newcandi.end(), pair<string, map<int, vector<int>, greater<int>>>(candi[i].first + str, count)) == newcandi.end())
newcandi.push_back(pair<string, map<int, vector<int>, greater<int>>>(candi[i].first + str, count));
if (newcandi.back().first.length() < minlen)
minlen = newcandi.back().first.length();
}
else
{
int single = -1;
vector<int>tochoose;
int max = -1;
for (int j = 0; j < candi[i].second.begin()->second.size(); j++)
{
if (nums[candi[i].second.begin()->second[j]].size() == 1)
single = j;
else if (nums[candi[i].second.begin()->second[j]][1] - '0' >= max)
{
if (nums[candi[i].second.begin()->second[j]][1] - '0' > max)
{
max = nums[candi[i].second.begin()->second[j]][1] - '0';
tochoose.clear();
tochoose.push_back(j);
}
else
tochoose.push_back(j);
}
}
if (max < candi[i].second.begin()->first&&single >= 0)
{

string str = nums[candi[i].second.begin()->second[single]];
map<int, vector<int>, greater<int>>count = candi[i].second;
count.begin()->second.erase(count.begin()->second.begin() + single, count.begin()->second.begin() + single + 1);
if (find(newcandi.begin(), newcandi.end(), pair<string, map<int, vector<int>, greater<int>>>(candi[i].first + str, count)) == newcandi.end())
newcandi.push_back(pair<string, map<int, vector<int>, greater<int>>>(candi[i].first + str, count));
if (newcandi.back().first.length() < minlen)
minlen = newcandi.back().first.length();

}
else
{
for (int h = 0; h < tochoose.size(); h++)
{
string str = nums[candi[i].second.begin()->second[tochoose[h]]];
map<int, vector<int>, greater<int>>count = candi[i].second;
count.begin()->second.erase(count.begin()->second.begin() + tochoose[h], count.begin()->second.begin() + tochoose[h] + 1);
if (find(newcandi.begin(), newcandi.end(), pair<string, map<int, vector<int>, greater<int>>>(candi[i].first + str, count)) == newcandi.end())
newcandi.push_back(pair<string, map<int, vector<int>, greater<int>>>(candi[i].first + str, count));
if (newcandi.back().first.length() < minlen)
minlen = newcandi.back().first.length();
}
if (max == candi[i].second.begin()->first&&single >= 0)
{
string str = nums[candi[i].second.begin()->second[single]];
map<int, vector<int>, greater<int>>count = candi[i].second;
count.begin()->second.erase(count.begin()->second.begin() + single, count.begin()->second.begin() + single + 1);
if (find(newcandi.begin(), newcandi.end(), pair<string, map<int, vector<int>, greater<int>>>(candi[i].first + str, count)) == newcandi.end())
newcandi.push_back(pair<string, map<int, vector<int>, greater<int>>>(candi[i].first + str, count));
if (newcandi.back().first.length() < minlen)
minlen = newcandi.back().first.length();
}
}
}
}
if (newcandi.size()>1)
{
vector<pair<string, map<int, vector<int>, greater<int>>>>tempcandi;
string ss = string(newcandi.front().first.begin(), newcandi.front().first.begin() + minlen);
for (int g = 0; g < newcandi.size(); g++)
{
string sss = string(newcandi[g].first.begin(), newcandi[g].first.begin() + minlen);
int re = sss.compare(ss);
if (re > 0)
{
ss = sss;
tempcandi.clear(); tempcandi.push_back(newcandi[g]);
}
else if (re==0)
tempcandi.push_back(newcandi[g]);
}
newcandi = tempcandi;
}
candi = newcandi;
}
string myitoa(int num)
{
string re;
if (num == 0)
return string("0");
while (num != 0)
{
re.insert(re.begin(), '0' + num % 10);
num = num / 10;
}
return re;
}
public:
string largestNumber(vector<int>& nums) {
string re;
if (nums.empty())
return re;
bool f = false;
for (int i = 0; i < nums.size(); i++)
if (nums[i] != 0)
{
f = true;
break;
}
if (!f)
return string("0");
vector<string>strnums;
map<int, vector<int>, greater<int>>count;
for (int i = 0; i < nums.size(); i++)
{
strnums.push_back(myitoa(nums[i]));
count[strnums[i][0] - '0'].push_back(i);
}
vector<pair<string, map<int, vector<int>, greater<int>>>>candi;
candi.push_back(pair<string, map<int, vector<int>, greater<int>>>("", count));
int k = nums.size();
while (k>0)
{
k--;
do_once(strnums, candi);
}
if (candi.size()==1)
return candi[0].first;
else
{
string ss = candi[0].first;
for (int i = 1; i < candi.size(); i++)
if (candi[i].first.compare(ss) > 0)
ss = candi[i].first;
return ss;
}
}
};

accepted
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: