[剑指offer] 字符流中第一个不重复的字符

题目内容

请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。

https://www.nowcoder.com/practice/00de97733b8e4f97a3fb5c680ee10720?tpId=13&tqId=11207&tPage=3&rp=3&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking


题目思路

我们先建立一个初始化数组用来存储字符串,然后每次都append char。接下来从头开始进行判定,把符合条件的字符输出。


程序代码

# -*- coding:utf-8 -*-
class Solution:
    # 返回对应char
    def __init__(self):
        self.string=[]
    def FirstAppearingOnce(self):
        # write code here
        for i in self.string:
            if self.string.count(i)==1:
                return i 
        return '#'
    def Insert(self, char):
        # write code here
        self.string.append(char)

猜你喜欢

转载自blog.csdn.net/u010929628/article/details/92773538