法一:STL
#include <string>
#include <iostream>
using namespace std;
int main()
{
string s;
cin >> s;
for(int i = 0; i < s.size(); ++i)
{
// 相等,遇到的是同一个字符
if(s.find_first_of(s[i]) == s.find_last_of(s[i]))
{
cout << s[i] << endl;
break;
}
// i走到尽头
if(i == s.size() - 1)
{
cout << -1 << endl;
}
}
return 0;
}
法二:hash映射
对字符串中的每一个字符进行hash映射。
按字符串的顺序检查字符的出现次数,返回第一次出现一次的字符,否则,返回-1.