forked from fredfeng0326/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlet821.py
41 lines (36 loc) · 1.06 KB
/
let821.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
# 821. Shortest Distance to a Character
class Solution:
def shortestToChar(self, S, C):
"""
:type S: str
:type C: str
:rtype: List[int]
"""
list_c = []
for index,item in enumerate(S):
if C == item:
list_c.append(index)
list_return = []
for index,item in enumerate(S):
list_dif = []
for dif in list_c:
list_dif.append(abs(index - dif))
list_return.append(min(list_dif))
return list_return
def shortestToChar2(self, S, C):
"""
:type S: str
:type C: str
:rtype: List[int]
"""
list_c = []
for index, item in enumerate(S):
if C == item:
list_c.append(index)
list_return = []
for index, item in enumerate(S):
distance = [abs(index - index_list) for index_list in list_c]
list_return.append(min(distance))
return list_return
a = Solution()
print(a.shortestToChar('loveleetcode','e'))