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

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

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

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

int main() {
    string str;
    cin>>str;
    map<char,int> m;
    map<char,int>::iterator it;
    for(int i=0;i<str.size();i++){
        it = m.find(str[i]);
        if(it==m.end()){
            m.insert(pair<char,int>(str[i],1));
        }else{
            it->second++;
        }
    }
    int a ;
    for(a = 0;a<str.size();a++){
        it = m.find(str[a]);
        if(it->second==1){
            cout<<it->first;
            break;
        }
    }
    if(a>=str.size()){
        cout<<-1;
    }
    
}

// 64 位输出请用 printf("%lld")

全部评论

相关推荐

09-01 16:09
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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