offer50-2:字符流中第一个只出现一次的字符

找出字符流中第一个只出现一次的字符。例如,当从字符流google中只读出前两个字符go时,第一个只出现一次的字符是g;当读完google时,第一个只出现一次的字符是l。


class Solution():
    def __init__(self):
        #s存储读入字符流的字符
        self.s=[]
        #count存储字符出现的次数
        self.count={}
    def Insert(self,char):
        self.s+=char
        if char not in self.count:
            self.count[char]=1
        else:
            self.count[char]+=1
    def firstAppearingOnce(self):
        for i in range(len(self.s)):
            if self.count[self.s[i]]==1:
                return self.s[i]
        return '#'
s=Solution()
s.Insert('g')
s.Insert('o')
s.Insert('o')
s.Insert('g')
s.Insert('l')
s.Insert('e')
print(s.s)
print(s.count)
s.firstAppearingOnce()
发布了86 篇原创文章 · 获赞 1 · 访问量 8233

猜你喜欢

转载自blog.csdn.net/a1272899331/article/details/104483914