題目描述
請實現一個函數用來找出字符流中第一個只出現一次的字符。例如,當從字符流中只讀出前兩個字符"go"時,第一個只出現一次的字符是"g"。當從該字符流中讀出前六個字符“google"時,第一個只出現一次的字符是"l"。
輸出描述:
如果當前字符流沒有存在出現一次的字符,返回#字符。
題解
#include <iostream>
#include <map>
#include <vector>
using namespace std;
map<char, int> m;
vector<int> v;
void Insert(char ch) {
v.push_back(ch);
m[ch]++;
}
char FirstAppearingOnce() {
for (auto it = v.begin(); it != v.end(); ++it)
if (m[*it] == 1)
return *it;
return '#';
}
int main() {
ios::sync_with_stdio(false);
char c;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> c;
Insert(c);
}
cout << FirstAppearingOnce();
return 0;
}