剑指 第一个重复

字符流中第一个不重复的字符

http://www.nowcoder.com/questionTerminal/00de97733b8e4f97a3fb5c680ee10720

因为字符流,先进入的先处理,所以依靠队列存储,和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
全部评论

相关推荐

_mos_:要不是看评论区我都不知道你要找的是数分
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务