题解 | #字符流中第一个不重复的字符#

字符流中第一个不重复的字符

http://www.nowcoder.com/practice/00de97733b8e4f97a3fb5c680ee10720

{
public:
  //Insert one char from stringstream
    queue<char> q;
    map<char, int> mp;
    void Insert(char ch)
    {
      if(!mp[ch]){//如果是第一次出现,加入队列中
          q.push(ch);
      }
        mp[ch]++;//记录ch字符的个数
    }
  //return the first appearence once char in current stringstream
    char FirstAppearingOnce()
    {
     while(!q.empty()){
         char temp=q.front();
         if(mp[temp]==1){//证明第一次出现,直接返回该字符
             return temp;
         }
         else{//不是首个字母,那么就得出栈
             q.pop();
         }
     }
         return '#';//其他情况直接返回
    }
};
全部评论

相关推荐

每晚夜里独自颤抖:这个在牛客不是老熟人了吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务