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 - 字符的最短距离
寻找字符串中最近字符距离
最新推荐文章于 2025-03-30 21:47:33 发布
本文介绍了一种算法,用于解决在给定字符串S中找到每个位置到指定字符C的最短距离的问题。通过记录所有目标字符的位置并计算每个字符到这些位置的距离,实现了高效的解决方案。
402

被折叠的 条评论
为什么被折叠?



