题解 | #字符流中第一个不重复的字符#
字符流中第一个不重复的字符
https://www.nowcoder.com/practice/00de97733b8e4f97a3fb5c680ee10720
#include <functional> #include <unordered_map> class Solution { public: //Insert one char from stringstream void Insert(char ch) { hash_map[ch] += 1; str += ch; } //return the first appearence once char in current stringstream char FirstAppearingOnce() { for (int i = 0; i < str.length(); ++i) { if (hash_map[str[i]] == 1) { return str[i]; } } return '#'; } private: string str; std::unordered_map<char, int> hash_map; };
把hash map用好即可
note_coding 文章被收录于专栏
记录自己的解题思路, 欢迎评价