题解 | #删除字符串中出现次数最少的字符#

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

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

感觉大家思路都差不多怎么我的时间就多那么多呢

#include <string>
#include <memory.h>
using namespace std;
int main()
{
    string str;
    int minValue;
    int a[26];
    while(cin >> str) {
        if (str.length() <= 0) {
            cout << "" << endl;;
            continue;
        }
        memset(a, 0, sizeof(a));
        for(int i = 0; i < str.length(); i++) {
            a[int(str[i] - 'a')] += 1;
        }
        minValue = a[int(str[0] - 'a')];
        //最小出现的次数
        for(int i = 0; i < sizeof(a)/sizeof(a[0]); i++) {
            if (a[i] > 0 && a[i] <= minValue) {
                minValue = a[i];
            }
        }
        
        //输出
        for (int i = 0; i < str.length(); i++) {
            if (a[int(str[i]-'a')] > minValue)
                cout << str[i];
        }
        cout << endl;
    }
}
全部评论

相关推荐

06-20 17:42
东华大学 Java
凉风落木楚山秋:要是在2015,你这简历还可以月入十万,可惜现在是2025,已经跟不上版本了
我的简历长这样
点赞 评论 收藏
分享
06-18 13:28
已编辑
门头沟学院 Web前端
爱睡觉的冰箱哥:《给予你300的工资》,阴的没边了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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