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

public class Solution {
    int count[] = new int[256];
    int index = 1; //可能有多个字母都只出现一次,index的作用是区分先后顺序
    //Insert one char from stringstream
    public void Insert(char ch)
    {
        if(count[ch]==0){
            count[ch] = index;
            index++;
        }
        else{
            count[ch] = -1;
        }
    }
  //return the first appearence once char in current stringstream
    public char FirstAppearingOnce()
    {
        int index = Integer.MAX_VALUE;
        char ch = '#';
        for(int i=0; i<256; i++){
            if(count[i]!=-1 && count[i]!=0 && count[i] < index){
                index = count[i];
                ch = (char)i;
            }
        }
        return ch;
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容