class Solution
{
private:
std::vector<char> v;
public:
//Insert one char from stringstream
void Insert(char ch)
{
v.push_back(ch);
}
//return the first appearence once char in current stringstream
char FirstAppearingOnce()
{
std::map<char,int> m;
for (int i = 0; i < v.size(); ++i)
{
char ch = v.at(i);
if (m.count(ch)>0) m[ch]++;
else m[ch]=1;
}
for (int i = 0; i < v.size(); ++i)
{
char ch = v.at(i);
if (m[ch]==1) return ch;
}
return '#';
}
};
char类型被定义为8位的存储类型 可通过定义28长度的数组散列
class Solution
{
private:
char* v = new char[256]();
int now = 1;
public:
//Insert one char from stringstream
void Insert(char ch)
{
if (*(v+ch)== 0){
*(v+ch) = now;
now ++;
}
else
*(v+ch) = -1;
}
//return the first appearence once char in current stringstream
char FirstAppearingOnce()
{
int temp = 257;
char res;
for (int i = 0;i<256;i++){
if (*(v+i)>0&&temp>*(v+i)) {
temp = *(v+i);
res = i;
}
}
if(temp==257)
return '#';
return res;
}
};