解题思路:
(1)使用map和unordered_map
(2)注意由于map和unordered_map不是按照顺序存储的,注意判断语句
class Solution
{
public:
unordered_map<char,pair<int,int>> mp;
int count = 0;
//Insert one char from stringstream
void Insert(char ch){
if(mp.find(ch)==mp.end()) mp[ch].second=count;
count++;
mp[ch].first++;
}
//return the first appearence once char in current stringstream
char FirstAppearingOnce() {
int min = INT_MAX;
char c='#';
for(auto it=mp.begin();it!=mp.end();it++) {
if(it->second.first==1&&it->second.second<min) {
min = it->second.second;
c = it->first;
}
}
return c;
}
};