题解 | 找出字符串中第一个只出现一次的字符
找出字符串中第一个只出现一次的字符
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")
查看21道真题和解析