题解
AC-Code
class Solution {
public:
//Insert one char from stringstream
queue<char> q;
unordered_map<char, int> mp;
void Insert(char ch) {
// 如果是第一次出现, 则添加到队列中
if (mp.find(ch) == mp.end())
q.push(ch);
// 不管是不是第一次出现,都进行计数
++mp[ch];
}
//return the first appearence once char in current stringstream
char FirstAppearingOnce() {
while (!q.empty()) {
char ch = q.front();
// 拿出头部,如果是第一次出现,则返回
if (mp[ch] == 1)
return ch;
// 不是第一次出现,则弹出,然后继续判断下一个头部
else
q.pop();
}
return '#';
}
};