Practice44:
请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。
S1
本体重点在于想到用统计的map和计算顺序的queue
class Solution
{
public:
queue<char> q;
map<char,int> mp;
//Insert one char from stringstream
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 mid = q.front();
if(mp[mid]==1){
return mid;
}
q.pop();
}
return '#';
}
};

博客围绕Practice44展开,要求实现一个函数找出字符流中第一个只出现一次的字符,如读入‘go’时为‘g’,读入‘google’时为‘l’,重点是运用统计的map和计算顺序的queue来解决问题。
712





