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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u010592415/article/details/45508303


class Solution
{
public:
	int num[256];
    int index;
    Solution(){
        for(int i = 0; i < 256; i++)
            num[i] = -1;
        index = 0;
    }
  	//Insert one char from stringstream
	void Insert(char ch)
	{
		if(num[ch] == -1)
            num[ch] = index;
        else if(num[ch] >= 0)
            num[ch] = -2;
        index++;
	}
  	//return the first appearence once char in current stringstream
	char FirstAppearingOnce()
	{
        int minIndex = INT_MAX;
        char result = '#';
		for(int i = 0; i < 256; i++){
            if(num[i] >= 0 && num[i] < minIndex){
                minIndex = num[i];
                result = i;
            }
        }
        return result;
	}
};


展开阅读全文

没有更多推荐了,返回首页