剑指offer54:字符流中第一个不重复的字符

1 题目描述

  请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。

输出描述:

  如果当前字符流没有存在出现一次的字符,返回#字符。

2 思路和方法, C++核心代码 

 1 class Solution
 2 {
 3 public:
 4   //Insert one char from stringstream
 5     void Insert(char ch)
 6     {
 7         s = s + ch;
 8         if(cha[ch])
 9             cha[ch]++;
10         else
11             cha[ch] = 1;
12     }
13   //return the first appearence once char in current stringstream
14     char FirstAppearingOnce()
15     {
16         int length = s.size();
17         for(int i=0; i<length; i++){
18             if(cha[s[i]] == 1)
19                 return s[i];
20         }
21         return '#';
22     }
23 private:
24     char cha[256]={0};
25     string s;
26 };
View Code

参考资料

https://blog.csdn.net/u012477435/article/details/83351659#_1529

原文地址:https://www.cnblogs.com/wxwhnu/p/11429054.html