题目描述
请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。
输出描述:
如果当前字符流没有存在出现一次的字符,返回#字符。
看着题有点眼熟,因为前面做过了,字符串中第一个不重复的字符。
差别就是字符流是一个一个读出的;例如:google 题目为字符串则输出 l 题目为字符流则输出ggg#ll 每读入一个新的字符相当于一个新的字符串来判断
代码:
class Solution { public: int arr[258]; int index = 0; Solution() { for(int i = 0;i < 256;i++) arr[i] = -1; } //Insert one char from stringstream void Insert(char ch) { if(arr[ch] == -1) arr[ch] = index; else arr[ch] = -2; index++; } //return the first appearence once char in current stringstream char FirstAppearingOnce() { int res = index+1; char ch = '