【剑指Offer】字符流中第一个不重复的字符 解题报告(Python)

【剑指Offer】字符流中第一个不重复的字符 解题报告(Python)

标签(空格分隔): 剑指Offer


题目地址:https://www.nowcoder.com/ta/coding-interviews

题目描述:

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

输出描述:

如果当前字符流没有存在出现一次的字符,返回#字符。

解题方法

很明显使用字典统计下出现的次数即可。

代码:

# -*- coding:utf-8 -*-
from collections import Counter
class Solution:
    def __init__(self):
        self.words = []
    # 返回对应char
    def FirstAppearingOnce(self):
        remm = Counter(self.words)
        for char in self.words:
            if remm[char] == 1:
                return char
        return '#'
    def Insert(self, char):
        self.words.append(char)

Date

2018 年 3 月 28 日 – 北京雾霾+沙尘暴,我的天。。

posted @ 2018-03-28 21:43  负雪明烛  阅读(36)  评论(0)    收藏  举报