题解 | #删除字符串中出现次数最少的字符#C++解法,优化后的简洁代码

删除字符串中出现次数最少的字符

http://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string str;
    while(cin>>str)
    {
        map<char,int> m;
        for(auto x:str)
        {
            m[x]++;//用map统计字符个数
        }
        vector<char> vc;
        int tmp=m.begin()->second;
        for(auto it=m.begin();it!=m.end();it++)
        {
           if(tmp>it->second)
           {
               tmp=it->second;//最小的个数是tmp个
           }
        }
        for(auto x:str)
        {
            if(m[x]>tmp)//将str中出现次数大于tmp个的字符都输出
            {
                cout<<x;
            }
        }
       cout<<endl;
    }
}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务