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

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

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 '#';
    }

};

全部评论

相关推荐

野猪不是猪🐗:还是太卑微了,什么叫放弃本次面试应该说经过评估,贵公司与自己不匹配,决定不再推进后续流程
点赞 评论 收藏
分享
09-29 16:59
已编辑
门头沟学院 Java
牛客96609213...:疯狂背刺,之前还明确设置截止日期,还有笔试,现在一帮人卡在复筛,他反而一边开启扩招,还给扩招的免笔试,真服了,你好歹先把复筛中的给处理了再说
投递大疆等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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