剑指offer:字符流中第一个不重复的字符

试题:
请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。
代码:
我们需要一个hash表,既能存储每个字符出现位置,又能统计字符出现次数。因为如果是第一次出现,那么我们就记录为index,如果第二次出现,我们就用一个数值表示出现次数超过1.

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

你可能感兴趣的:(sword2offer)