题解 | #字符流中第一个不重复的字符#
字符流中第一个不重复的字符
https://www.nowcoder.com/practice/00de97733b8e4f97a3fb5c680ee10720
class Solution { public: vector<char> v; // 存储字符流 vector<char> first; // 存储当前只出现一次的字符 //Insert one char from stringstream void Insert(char ch) { v.push_back(ch); if (v.size() == 1) { first.push_back(ch); } else { for (int i = 0; i < v.size() - 1; ++i) { if (ch == v[i]) { // 删除指定值的项 first.erase(remove(first.begin(), first.end(), ch), first.end()); break; } if (i == v.size() - 2) { first.push_back(ch); } } } } //return the first appearence once char in current stringstream char FirstAppearingOnce() { if (first.size() == 0) { return '#'; } else { return first.front(); } } };