JZ54 劍指offer 字元流中第一個不重複的字元
阿新 • • 發佈:2021-02-06
技術標籤:劍指
第54題 字元流中第一個不重複的字元
題目描述
請實現一個函式用來找出字元流中第一個只出現一次的字元。例如,當從字元流中只讀出前兩個字元"go"時,第一個只出現一次的字元是"g"。當從該字元流中讀出前六個字元“google"時,第一個只出現一次的字元是"l"。
class Solution
{
public:
//Insert one char from stringstream
unordered_map<char,int> m;
queue<char> q;
void Insert(char ch)
{
if(m.find(ch) == m.end()){
q.push(ch);
}
++m[ch];
}
//return the first appearence once char in current stringstream
char FirstAppearingOnce()
{
while(!q.empty()){
char c = q.front();
if(m[ c]==1){
return c;
}else{
q.pop();
}
}
return '#';
}
};