C++ 代码
class Solution{
public:
//Insert one char from stringstream
void insert(char ch){
if (cnt[ch] <= 1) {
cnt[ch]++;
}
if (cnt[ch] == 1) {
q.push(ch);
}
}
//return the first appearence once char in current stringstream
char firstAppearingOnce(){
while (!q.empty()) {
char ch = q.front();
if (cnt[ch] == 1) {
return ch;
} else {
q.pop();
}
}
return '#';
}
private:
int cnt[256] = {0};
queue<char> q;
};