思路

使用队列和统计数组来解题,统计数组用来保存字符的出现次数

Code

import java.util.LinkedList;
import java.util.Queue;

public class Solution {
    private int[] cnts = new int[128];
    private Queue<Character> queue = new LinkedList<>();
    //Insert one char from stringstream
    public void Insert(char ch)
    {
        cnts[ch]++;
        queue.add(ch);
        while(!queue.isEmpty() && (cnts[queue.peek()] > 1))
            queue.poll();
    }
  //return the first appearence once char in current stringstream
    public char FirstAppearingOnce()
    {
        return queue.isEmpty() ? '#' : queue.peek();
    }
}