因为字符流,先进入的先处理,所以依靠队列存储,和hashmap记数。
insert函数插入字符,如果原先队列里没有就直接插入,如果有了就不插入了,然后使用hashmap记数,在first函数中,如果队列开头元素没重复,则输出,如果开头元素重复了则弹出,再继续看下一个元素。
class Solution: # 返回对应char def __init__(self): self.queue=[] self.hashmap={} def FirstAppearingOnce(self): # write code here while self.queue: value=self.queue[0] if self.hashmap[value]==1: return self.queue[0] else: self.queue=self.queue[1:] return "#" def Insert(self, char): # write code here if char not in self.queue: self.queue.append(char) self.hashmap[char]=1 else: self.hashmap[char]+=1