剑指 第一个重复

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

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
全部评论

相关推荐

03-29 18:59
运城学院 Java
程序员小白条:咱们要对自己的简历和学历有清晰的认知,不要动不动就大厂了....都26届了,没实习还想着大厂,唉
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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