文章出處
文章列表
題目描述
請實現一個函數用來找出字符流中第一個只出現一次的字符。例如,當從字符流中只讀出前兩個字符"go"時,第一個只出現一次的字符是"g"。當從該字符流中讀出前六個字符“google"時,第一個只出現一次的字符是"l"。
輸出描述:
如果當前字符流沒有存在出現一次的字符,返回#字符。
class Solution
{
private:
vector<char> vec;
map<char, int> mapper;
public:
//Insert one char from stringstream
void Insert(char ch)
{
vec.push_back(ch);
mapper[ch] ++;
}
//return the first appearence once char in current stringstream
char FirstAppearingOnce()
{
for(int i=0; i<vec.size(); i++){
if(mapper[vec[i]]==1){
return vec[i];
}
}
return '#';
}
};
文章列表
全站熱搜