题目描述:
请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。
解题思路:
用哈希表存储每个字符以及出现的次数,如果某个字符出现的次数等于1,则返回。
Demo:
class Solution
{
public:
string str;
unordered_map<char, int> mp;
void Insert(char ch){
str += ch;
mp[ch]++;
}
char FirstAppearingOnce(){
for (const auto &s : str){
auto it = mp.find(s);
if (it->second == 1)
return it->first;
}
return '#';
}
};