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

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

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

【思路】

首先我们建立一个长度为256的数组,由于字符串中每个字符的范围是0~255,因此索引号相当于key, 而次数相当于value,再使用一个string类型记录整个字符串,当遍历完后,我们再次遍历整个字符串,寻找值为1的元素,返回其索引值就行!

char虽然是一个字符,其实质为一个8bit的空间,因此可以储存数字为0-255.

class Solution
{
private:
    string str;
    int ss[256] = {0};
public:
  //Insert>   
void Insert(char ch)
{ str += ch; ss[ch]++; } //return the first appearence>
char FirstAppearingOnce()
{ int len = str.length(); for(int i = 0; i < len;i++){ if(ss[str[i]] == 1){ return str[i]; } } return '#'; } };
原文地址:https://www.cnblogs.com/zhudingtop/p/11456028.html