题解 | #找出字符串中第一个只出现一次的字符#
找出字符串中第一个只出现一次的字符
https://www.nowcoder.com/practice/e896d0f82f1246a3aa7b232ce38029d4
#include <iostream>
#include <unordered_map>
#include <vector>
#include <string>
using namespace std;
// 一眼哈希
int main() {
string str;
unordered_map<char,int>hashMap;
vector<char>keys;
getline(cin,str);
for(auto ch:str)
{
if(hashMap.find(ch) == hashMap.end())
{
hashMap[ch] = 1;
keys.push_back(ch);
}
else {
hashMap[ch]++;
}
}
/*
for(auto it = hashMap.begin();it!=hashMap.end();++it)
{
if(it->second == 1)
{
cout << it->first << endl;
break;
}
}
*/
int i =0;
for( i = 0;i<keys.size();i++)
{
if(hashMap[keys[i]] == 1)
{
cout << keys[i] << endl;
break;
}
}
if(i == keys.size() && hashMap[keys[i]] != 1)
{
cout << -1 << endl;
}
}
// 64 位输出请用 printf("%lld")