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

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

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

#include <iostream>
#include <map>
using namespace std;

int main() {
    string str;
    while(cin>>str)
    {
        map<char, int> mp;
        string ans="";
        bool flag=false;
        for(int i=0;i<str.size();i++)
        {
            mp[str[i]]++;
        }
        for(auto it=mp.begin();it !=mp.end();it++)
        {
            if(it->second == 1)
            {
                ans+=it->first;
            }
        }
        char c;
        for(int i=0;i<str.size();i++)
        {
            if(ans.find(str[i])!=ans.npos)
            {
                c=str[i];
                flag=true;
                break;
            }
        }
        if(flag==true)
            cout<<c<<endl;
        else
            cout<<-1<<endl;
    }
    return 0;
}

全部评论

相关推荐

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