Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7499e15

Browse files
committed
Update array.md
最小覆盖子串
1 parent 819411e commit 7499e15

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

‎data_structure/array.md‎

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ class Solution:
262262
return res
263263
```
264264

265-
###[最小覆盖子串](https://leetcode.cn/problems/minimum-window-substring/)
265+
###[最小覆盖子串](https://leetcode.cn/problems/minimum-window-substring/)
266266
> 给你一个字符串 s 、一个字符串 t 。返回 s 中涵盖 t 所有字符的最小子串。如果 s 中不存在涵盖 t 所有字符的子串,则返回空字符串 "" 。
267267
>
268268
> **注意**:
@@ -271,7 +271,7 @@ class Solution:
271271
272272
思路一:利用Counter计数器,比较方便但耗时非常多
273273

274-
**Python版本**
274+
**Python版本一**
275275
```python
276276
from collections import Counter
277277
class Solution:
@@ -299,7 +299,39 @@ class Solution:
299299
return ""
300300
```
301301

302+
**Python版本二**
303+
```python
304+
from collections import Counter
305+
class Solution:
306+
def minWindow(self, s: str, t: str) -> str:
307+
ct = Counter()
308+
for ch in t: ct[ch] += 1
309+
need = len(t)
310+
start = 0
311+
res = len(s) + 1
312+
wind = [-1, -1]
313+
for end in range(len(s)):
314+
ch = s[end]
315+
if ch in ct:
316+
if ct[ch] > 0: need -= 1
317+
ct[ch] -= 1
318+
319+
while need == 0 and start <= end:
320+
subL = end - start + 1
321+
if res > subL:
322+
res = subL
323+
wind = [start, end+1]
324+
ch = s[start]
325+
if ch in ct:
326+
if ct[ch] == 0: need += 1
327+
ct[ch] += 1
328+
start = start + 1
329+
330+
if res != len(s) + 1:
331+
return s[wind[0]:wind[1]]
332+
else: return ""
302333

334+
```
303335

304336

305337

@@ -337,4 +369,7 @@ vector<int> spiralOrder(vector<vector<int>>& matrix) {
337369
- [ ] [移动零](https://leetcode.cn/problems/move-zeroes/)
338370
- [ ] [有序数组的平方](https://leetcode.cn/problems/squares-of-a-sorted-array/)
339371
- [ ] [比较含退格的字符串](https://leetcode.cn/problems/backspace-string-compare/)
340-
- [ ] [spiral-matrix](https://leetcode-cn.com/problems/spiral-matrix/)
372+
- [ ] [spiral-matrix](https://leetcode-cn.com/problems/spiral-matrix/)
373+
- [ ] [长度最小的子数组](https://leetcode.cn/problems/minimum-size-subarray-sum/)
374+
- [ ] [水果成篮](https://leetcode.cn/problems/fruit-into-baskets/)
375+
- [ ] [最小覆盖子串](https://leetcode.cn/problems/minimum-window-substring/)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /