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

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

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

#include <map>
#include <queue>
class Solution
{
public:
  //Insert one char from stringstream
    
    queue<char> q;
     map<char, int> mp;

    void Insert(char ch) {
    
        mp[ch] ++;

        if ( mp[ch] == 1 ) {

            q.push(ch);

        } // 维护哈希表判断是否只出现一次

        while ( !q.empty() && mp[q.front()] != 1 ) {

            q.pop();

        } // 维护队列找出符合条件的第一个字符 

    }
  //return the first appearence once char in current stringstream
    char FirstAppearingOnce() {
        
        return q.empty() ? '#' : q.front();

    }

};

全部评论

相关推荐

11-04 10:30
已编辑
门头沟学院 研发工程师
开心小狗🐶:“直接说答案”
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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