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

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

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

//用map<char, int>秒了
#include <iostream>
#include <algorithm>
#include <math.h>
#include <bits/stdc++.h>
#include <vector>
using namespace std;

int main() {
    string s;
    cin>>s;
    int n= s.size();
    map<char, int> a;
    for(int i=0; i<n;i++){
        a[s[i]] +=1;
    }
    int l= 0;
    for(char i=0; i<n;i++){
        if(a[s[i]] == 1){
            cout<<s[i];
            l+=1;
            return 0;
        }
    }
    if(l==0) {
        cout<<"-1";
    }
    
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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