题解 | #找出字符串中第一个只出现一次的字符#
找出字符串中第一个只出现一次的字符
https://www.nowcoder.com/practice/e896d0f82f1246a3aa7b232ce38029d4
#include<iostream>
#include<string>
#include<vector>
#include<queue>
using namespace std;
int main(){
vector<char> res;
vector<int>bucket(26,0);
string str;
string ans = "-1";
cin >>str;
for(char c : str){
// cout << "当前字符:" << c << endl;
// cout << "是否出现过:" << bucket[c-'a'] << endl;
if( bucket[ c - 'a' ] == 0 ){
bucket[ c - 'a' ]++;
res.push_back(c);
// cout << "加入结果集:" << endl;
// for(auto x : res)
// cout << x << ' ';
// cout << endl;
}
else{
for(int i = 0; i < res.size(); i++){
if( res[i] == c ){
res[i] = '*';
break;
}
}
// cout << "当前结果集:" << endl;
// for(auto x : res)
// cout << x << ' ';
// cout << endl;
}
}
for(char cur : res ){
if( isalpha(cur) ){
ans = cur ;
break;
}
}
cout <<ans;
}
查看12道真题和解析