题解 | #找出字符串中第一个只出现一次的字符#
找出字符串中第一个只出现一次的字符
https://www.nowcoder.com/practice/e896d0f82f1246a3aa7b232ce38029d4
#include <cstring>
#include <iostream>
using namespace std;
int main() {
string str;
cin >> str;
int hash[128]; //使用hash表
memset(hash,0,sizeof(hash)); //初始化hash表
for (int i = 0;i < str.size();i++)
{
hash[str[i]] += 1;
}
char s; //记录第一个只出现一次的字符
for (int i = 0;i < str.size();i++)
{
if (hash[str[i]] == 1)
{
s = str[i];
break;
}
}
if (s != NULL)
{
cout << s << endl;
}
else
{
cout << -1 << endl;
}
}
// 64 位输出请用 printf("%lld")
