- 一个哈希表储存字符出现次数,一个队列储存第一次出现的字符
- 输入时记录字符出现次数,第一次出现则加入队列
- 输出时遍历队列,遇到只在哈希表中出现过一次的字符就输出,没有就输出#
import java.util.*;
public class Solution {
Map<Character, Integer> cache = new HashMap();
Queue<Character> queue = new LinkedList();
//Insert one char from stringstream
public void Insert(char ch)
{
// 不存在就进对列,存缓存
if(!cache.containsKey(ch)) {
cache.put(ch,1);
queue.offer(ch);
// 存在就加次数
} else {
cache.put(ch, cache.get(ch) + 1);
}
}
//return the first appearence once char in current stringstream
public char FirstAppearingOnce()
{
for(Character ch : queue) {
if(cache.get(ch) == 1) {
return ch;
}
}
return '#';
}
}