题解 | #字符流中第一个不重复的字符# 基于哈希表

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

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

#include <unordered_map>
class Solution
{
public:
  // 需要注意的是FirstAppearingOnce() 函数没有输入参数,所以需要定义类的成员变量。
    unordered_map<char, int> map;
    string path;
    int loc = 0;
  //Insert one char from stringstream
    void Insert(char ch) {
        path += ch;
         if(map.find(ch) == map.end()){
            map[ch] = 1;
         }else{
            map[ch]++;
         }
    }
  //return the first appearence once char in current stringstream
    char FirstAppearingOnce() {
        while(loc < path.size()){
            if(map.find(path[loc]) != map.end()){
                if(map[path[loc]] == 1){
                    return path[loc];
                }else{
                    loc++;
                }
            }
        }
        return '#';
    }

};

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-07 14:00
不想多说了,什么逆天HR,还要教我礼貌😂
机械打工仔:这不纯傻卵吗,他还操心上别人老板了
投递BOSS直聘等公司7个岗位
点赞 评论 收藏
分享
点赞 评论 收藏
分享
不亏是提前批,神仙打架,鼠鼠不配了
站队站对牛:现在92都报工艺岗了
投递韶音科技等公司8个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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