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

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

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

class Solution
{
public:
  //Insert one char from stringstream
    void Insert(char ch) {
        if(canNotInsert.find(ch)==canNotInsert.end())
        {
            if(used.find(ch)==used.end())
            {
                used.insert({ch, cnt++});
            }
            else 
            {
                used.erase(ch);
                canNotInsert.insert(ch);
            }
        }
    }
  //return the first appearence once char in current stringstream
    char FirstAppearingOnce() {
        char res = '#';
        int minT = 10000;
        for(auto item:used)
        {
            if(item.second<minT)
            {
                res = item.first;
                minT = item.second;
            }
        }
        return res;
    }

    unordered_map<char,int> used;
    unordered_set<char> canNotInsert;
    char val;
    int cnt;
};

全部评论

相关推荐

upLuck:比我强查看图片
点赞 评论 收藏
分享
04-28 10:14
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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