Fork me on GitHub

字符流中第一个不重复的字符

题目描述

请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符”go”时,第一个只出现一次的字符是”g”。当从该字符流中读出前六个字符“google”时,第一个只出现一次的字符是”l”。

输出描述:

如果当前字符流没有存在出现一次的字符,返回#字符。

解决方案

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution
{
public:
//Insert one char from stringstream
void Insert(char ch)
{
hashtable[ch]++;
s = s+ch;
}
//return the first appearence once char in current stringstream
char FirstAppearingOnce()
{
for(char c :s){
if(hashtable[c]==1)
return c;
}
return '#';
}
private:
int hashtable[256];
string s;
};

本文标题:字符流中第一个不重复的字符

文章作者:LiuXiaoKun

发布时间:2019年03月29日 - 23:03

最后更新:2019年03月29日 - 23:03

原始链接:https://LiuZiQiao.github.io/2019/03/29/字符流中第一个不重复的字符/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

0%