题解 | #找出字符串中第一个只出现一次的字符#

找出字符串中第一个只出现一次的字符

https://www.nowcoder.com/practice/e896d0f82f1246a3aa7b232ce38029d4

#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;

int main() {
    unordered_map<char, int> count;
    string str;
    string min;
    char ch = '\0';
    cin >> str;
    for (auto x : str)
    {
        count[x]++;
    }
    for (auto p : count)
    {
        if (p.second == 1)
        {
            min += p.first;
        }
    }
    if (min.size() == 0)
    {
        cout << -1;
    }
    else 
    {
        for (auto x : str)
        {
            for (auto c : min)
            {
                if (x == c)
                {
                    ch = x;
                    break;
                }
            }
            if (ch != '\0')
            {
                cout << ch;
                break;
            }
        }
    }
    return 0;
}

全部评论

相关推荐

09-12 11:55
已编辑
湖南工商大学 Java
那一天的Java_J...:这种一堆问题的,别去
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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