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

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

https://www.nowcoder.com/practice/00de97733b8e4f97a3fb5c680ee10720?tpId=265&tqId=39269&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3Fdifficulty%3D3%26page%3D1%26pageSize%3D50%26search%3D%26tpId%3D13%26type%3D265&difficulty=3&judgeStatus=undefined&tags=&title=

class Solution
{
public:
  //Insert one char from stringstream
    queue<char>q; // 队列记录字符次序
    map<char, int>mymap; // map记录字符数量
    void Insert(char ch) {
        mymap[ch] ++;
        if(mymap[ch] == 1) q.push(ch);
    }
  //return the first appearence once char in current stringstream
    char FirstAppearingOnce() {
        char top = q.front();
        while(! q.empty() && mymap[top] != 1){ // 寻找处于队头且数量为1的字符
            q.pop();
            top =q.front();
        }
        return q.empty() ? '#' : top;
    }

};
全部评论

相关推荐

不愿透露姓名的神秘牛友
今天 12:05
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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