class Solution{
public:
//Insert one char from stringstream
queue<char> q;
unordered_map<char,int> hash;
void insert(char ch){
hash[ch]++;
if(hash[ch]>1){
while(q.size()&&hash[q.front()]>1) q.pop();
}else q.push(ch);
}
//return the first appearence once char in current stringstream
char firstAppearingOnce(){
if(q.empty())return '#';
return q.front();
}
};
思路2:暴力
代码:
class Solution{
public:
//Insert one char from stringstream
string s;
unordered_map<char,int> hash;
void insert(char ch){
hash[ch]++;
s+=ch;
}
//return the first appearence once char in current stringstream
char firstAppearingOnce(){
for(auto x:s) if(hash[x]==1) return x;
return '#';
}
};