题目描述
请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符”go”时,第一个只出现一次的字符是”g”。当从该字符流中读出前六个字符“google”时,第一个只出现一次的字符是”l”。如果当前字符流没有存在出现一次的字符,返回#字符。
解题思路
代码
Python(2.7.3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18# -*- coding:utf-8 -*-
class Solution:
# 返回对应char
def __init__(self):
self.str = ""
self.data = {}
def FirstAppearingOnce(self):
# write code here
for i in self.str:
if self.data[i] == 1:
return i
return '#'
def Insert(self, char):
# write code here
self.str += char
self.data[char] = self.data.setdefault(char, 0) + 1运行时间:23ms
占用内存:5624k