题解 | #删除字符串中出现次数最少的字符#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;
    }
}
全部评论

相关推荐

06-11 17:39
门头沟学院 Java
小呆呆的大鼻涕:卧槽,用户彻底怒了
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-25 20:45
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务