给定两个字符串s和t长度分别是m和n返回 s 中的最短窗口 子串使得该子串包含t中的每一个字符包括重复字符。如果没有这样的子串返回空字符串。测试用例保证答案唯一。滑动窗口方法一class Solution(object): def minWindow(self, s, t): if len(t)len(s): return left0 result t_count[0]*128 s_count[0]*128 min_lenfloat(inf) for c in t: t_count[ord(c)]1 for right in range(len(s)): s_count[ord(s[right])]1 while all(s_count[i]t_count[i] for i in range(128)): if min_lenright-left1: min_lenright-left1 results[left:right1] s_count[ord(s[left])]-1 left1 return result方法二用一个变量记录字符的种类class Solution(object): def minWindow(self, s, t): if len(t)len(s): return left0 result t_count[0]*128 s_count[0]*128 formed0 min_lenfloat(inf) for c in t: t_count[ord(c)]1 need_chars sum(1 for i in range(128) if t_count[i] 0) for right in range(len(s)): cord(s[right]) s_count[c]1 if t_count[c]0 and t_count[c]s_count[c]: formed1 while formedneed_chars: cur_lenright-left1 if cur_lenmin_len: min_lencur_len results[left:right1] cur_cord(s[left]) s_count[cur_c]-1 if s_count[cur_c]t_count[cur_c]: formed-1 left1 return result