-
Notifications
You must be signed in to change notification settings - Fork 79
/
53.py
53 lines (47 loc) · 1.46 KB
/
53.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# -*- coding:utf-8 -*-
'''
请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。
输出描述:
如果当前字符流没有存在出现一次的字符,返回#字符。
'''
class Solution:
def __init__(self):
self.array = [-1 for i in range(256)]
self.index = 0
# 返回对应char
def FirstAppearingOnce(self):
i = 0
min = -1
index = 0
for itemArray in self.array:
if itemArray != -1 and itemArray != -2:
if min == -1:
min = itemArray
index = i
elif itemArray < min:
min = itemArray
index = i
i += 1
if min == -1:
return '#'
return chr(index)
def Insert(self, char):
intChar = ord(char)
if self.array[intChar] > -1:
self.array[intChar] = -2
else:
self.array[intChar] = self.index
self.index += 1
s = Solution()
s.Insert('g')
print s.FirstAppearingOnce()
s.Insert('o')
print s.FirstAppearingOnce()
s.Insert('o')
print s.FirstAppearingOnce()
s.Insert('g')
print s.FirstAppearingOnce()
s.Insert('l')
print s.FirstAppearingOnce()
s.Insert('e')
print s.FirstAppearingOnce()