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

    科技2022-07-11  93

    #include<unordered_map> #include<queue> using namespace std; class Solution { public: //Insert one char from stringstream //队列元素无重复,且保证字符顺序 queue<char> q; //统计每个字符出现的次数 unordered_map<char, int> um; //插入字符 void Insert(char ch) { //队列只进入不重复的字符 if (um.find(ch) == um.end()) q.push(ch); //统计个数 um[ch]++; } //return the first appearence once char in current stringstream char FirstAppearingOnce() { while (q.empty() == 0) { //依据队列顺序查找第一个不重复的字符 char tmp = q.front(); if (um[tmp] == 1) return tmp; else q.pop(); } return '#'; } };
    Processed: 0.014, SQL: 8