題目描述
請(qǐng)實(shí)現(xiàn)一個(gè)函數(shù)用來(lái)找出字符流中第一個(gè)只出現(xiàn)一次的字符克伊。例如酥郭,當(dāng)從字符流中只讀出前兩個(gè)字符"go"時(shí),第一個(gè)只出現(xiàn)一次的字符是"g"愿吹。當(dāng)從該字符流中讀出前六個(gè)字符“google"時(shí)不从,第一個(gè)只出現(xiàn)一次的字符是"l"。
輸出描述:
如果當(dāng)前字符流沒(méi)有存在出現(xiàn)一次的字符犁跪,返回#字符椿息。
class Solution
{
public:
string str="";
// vector<int> hash(256,0);
int hash[256];
int len = 0;
//Insert one char from stringstream
void Insert(char ch)
{
len ++;
str += ch;
hash[ch-'\0'] ++;
}
//return the first appearence once char in current stringstream
char FirstAppearingOnce()
{
for(int i=0;i<len;i++)
{
if(hash[str[i]-'\0']==1)
return str[i];
}
return '#';
}
};