**题目:**请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。
思路:利用一个向量存储字符流中的数据,用哈希表存储字符出现的次数。
class Solution
{
public:
//Insert one char from stringstream
void Insert(char ch)
{
if(ch!='\0')
{
st.push_back(ch);
if(m.count(ch))
m[ch]++;
else
m[ch]=1;
}
}
//return the first appearence once char in current stringstream
char FirstAppearingOnce()
{
for(int i=0;i<st.size();++i)
{
if(m[st[i]]==1)
return st[i];
}
return '#';
}
private:
vector<char> st;
map<char,int> m;
};