class Solution:
def shortestToChar(self, S, C):
"""
:type S: str
:type C: str
:rtype: List[int]
"""
index_C = []
distance = []
for i in range(len(S)):
if S[i] == C:
index_C.append(i)
for j in range(len(S)):
dist = min(abs(j - ind) for ind in index_C)
distance.append(dist)
return distance
leetcode - 821 - 字符的最短距离
最新推荐文章于 2021-06-14 19:42:33 发布