JZ54
字符流中第一个不重复的字符
http://www.nowcoder.com/questionTerminal/00de97733b8e4f97a3fb5c680ee10720
思路
使用队列和统计数组来解题,统计数组用来保存字符的出现次数
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();
}
} 