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

2019-06-05  本文已影响0人  quiterr
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;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读