题解 | #找出字符串中第一个只出现一次的字符#
找出字符串中第一个只出现一次的字符
https://www.nowcoder.com/practice/e896d0f82f1246a3aa7b232ce38029d4
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int findByKey(vector<pair<char,int>> v,char key){
for(int i=0;i<v.size();i++){
if(v[i].first==key){
return i;
}
}
return -1;
}
int findByValue(vector<pair<char,int>> v,int value){
for(int i=0;i<v.size();i++){
if(v[i].second==value){
return i;
}
}
return -1;
}
int main() {
string input;
while (cin >> input) { // 注意 while 处理多个 case
vector<pair<char,int>> v;
for(auto & i:input){
if(v.empty() || findByKey(v, i)==-1){
v.push_back(make_pair(i,1));
}else {
int j=findByKey(v, i);
v[j].second++;
}
}
int i=findByValue(v, 1);
if(i!=-1){
cout<<v[i].first<<endl;
}else {
cout<<i<<endl;
}
}
}
// 64 位输出请用 printf("%lld")

