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

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

思路:基于map的思路,,类型题

 int[] table=new int[256];
    StringBuffer sb=new StringBuffer(); 
    public void Insert(char ch)
    {
        sb.append(ch);
        table[ch]++;
    }
  //return the first appearence once char in current stringstream
    public char FirstAppearingOnce()
    {
        char[] str=sb.toString().toCharArray();
        for(char c:str){
            if(table[c]==1)
                return c;
        }
        return '#';
    }

 

posted @ 2017-04-11 02:29  雪浪snowWave  阅读(94)  评论(0编辑  收藏  举报