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

相关推荐

今天 17:35
已编辑
济宁学院 Java
不想做程序员:面试官:蓝桥杯三等奖?你多去两次厕所都能拿二等吧
点赞 评论 收藏
分享
11-06 16:50
门头沟学院 Java
用微笑面对困难:word打字比赛二等奖的我,也要来凑合凑合
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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