《剑指Offer》之“字符流中第一个不重复的字符”

题目描述


请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符 “走出去” 时,第一个只出现一次的字符是“G “当从该字符流中读出前六个字符“谷歌 “时,第一个只出现一次的字符是” L“。

代码实现


class Solution
{
public:
    int all[200];
    string s;
  //Insert one char from stringstream
    void Insert(char ch)
    {      s = s + ch;
           all[ch]++;
    }
  //return the first appearence once char in current stringstream
    char FirstAppearingOnce()
    {    char res = '#';
        for(int i = 0;i<s.length();i++){
             if (all[s[i]] ==1){
                 res = s[i];
                 return res;
             }     
        }
         return res;
    }

};

猜你喜欢

转载自blog.csdn.net/opooc/article/details/79909140
今日推荐