eg:
"aaaabbbbbcccc"
存入map本身有序变成
a=4
b=5
c=4
希望从前到后
a b c
再从后到前
c b a
遍历过的数字就去掉,不复用,目前代码如下。希望大佬能帮忙看看是哪有问题,谢谢!
这里附上leetcode链接
https://leetcode.com/contest/biweekly-contest-21/problems/increasing-decreasing-string/
报错图片:
直接把下面的代码放到链接里打开即可调试
class Solution {
public:
string sortString(string s) {
if(s.empty()) return "";
map<char,int> mp;
for(int i=0;i<s.size();++i)
mp[s[i]]++;
for(auto &it:mp)
{
cout<<it.first<<" "<<it.second<<endl;
}
string ans="";
while(!mp.empty())
{
for(auto &it:mp)
{
ans+=it.first;
it.second--;
if(it.second==0) mp.erase(it.first);
}
for(auto &it:mp)
{
cout<<it.first<<" "<<it.second<<endl;
}
for(map<char,int>::reverse_iterator it=mp.rbegin();it!=mp.rend();++it)
{
cout<<"**"<<(*it).first<<endl;
ans+=it->first;
it->second--;
if(it->second==0) mp.erase(it->first);
}
cout<<ans<<endl;
for(auto &it:mp)
{
cout<<it.first<<" "<<it.second<<endl;
}
}
return ans;
}
};
- F#: Storing and mapping a list of functions
- 有没有方法即使程序最小化也能对其发送按键
- How to get coordinates of an svg?
- Efficiently chunk large vector into a vector of ve
- Theoretical clarification regarding maps and itera
感觉你没有理解题目的意思:
问题一:
问题二:
附上我的AC代码,供参考:
PS:如果提问后,有人回答了满意的答案,请及时给予采纳,这样这个社区才会越来越好,有问题随时沟通,谢谢