题目链接

​https://www.nowcoder.com/practice/e896d0f82f1246a3aa7b232ce38029d4?tpId=37&tqId=21282&tPage=3&rp=&ru=/ta/huawei&qru=/ta/huawei/question-ranking​

题目描述

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

输入描述:

输入一个非空字符串

输出描述:

输出第一个只出现一次的字符,如果不存在输出-1

示例1

输入

复制

asdfasdfo

输出

复制

o

题解:

#include <iostream>
#include <string>
using namespace std;
struct character{
int prior;
int num;
char data;
character(){
num = 0;
prior = -1;
}
};
int main(){
string s;
while (cin >> s){
character buf[130];
int prior = 0;
bool is_exist = false;
for (int i = 0; i < s.length(); i++){
buf[s[i]].num++;
buf[s[i]].data = s[i];
if (buf[s[i]].prior == -1){
buf[s[i]].prior = prior;
}
prior++;
}
int min = 131, min_i = 0;
for (int i = 0; i < 130; i++){
if (buf[i].num == 1){
is_exist = true;
if (buf[i].prior < min){
min = buf[i].prior;
min_i = i;
}
}
}
if (is_exist == true){
cout << buf[min_i].data << endl;
}
else{
cout << "-1" << endl;
}
}
return 0;
}