题解 | #字符流中第一个不重复的字符# 基于哈希表
字符流中第一个不重复的字符
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 '#';
}
};
查看2道真题和解析