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 435c10b

Browse files
minimum-window-substring (labuladong#1593)
1 parent fc4b58b commit 435c10b

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

‎多语言解法代码/solution_code.md

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42621,53 +42621,64 @@ class Solution {
4262142621
```
4262242622

4262342623
```go
42624-
// by chatGPT (go)
42625-
// 采用滑动窗口方法的 Go 解法
42624+
// by mario_huang (go)
4262642625
func minWindow(s string, t string) string {
42627-
// 将需要匹配的字符及其出现次数存入 need
42628-
// 将滑动窗口中的字符及出现次数存入 window
42629-
need, window := make(map[byte]int), make(map[byte]int)
42630-
for i := range t {
42631-
need[t[i]]++
42626+
need := map[byte]int{}
42627+
window := map[byte]int{}
42628+
for _, c := range []byte(t) {
42629+
need[c]++
4263242630
}
42633-
42634-
left, right := 0, 0 // 定义窗口的左右边界
42635-
valid := 0 // valid 存储满足 need 条件的字符个数
42636-
start, length := 0, math.MaxInt32 // 定义最小覆盖子串的起始位置及长度
42637-
42638-
for right < len(s) { // 当右边界小于 s 的长度时
42631+
left, right := 0, 0
42632+
valid := 0
42633+
// 记录最小覆盖子串的起始索引及长度
42634+
start, length := 0, math.MaxInt
42635+
/**<extend down -200>
42636+
![](../pictures/slidingwindow/1.png)
42637+
*/
42638+
for right < len(s) {
42639+
// c 是将移入窗口的字符
4263942640
c := s[right]
42641+
// 右移窗口
4264042642
right++
42641-
42642-
if _, ok := need[c]; ok { // 如果 c 是需要匹配的字符
42643+
// 进行窗口内数据的一系列更新
42644+
if _, ok := need[c]; ok {
4264342645
window[c]++
4264442646
if window[c] == need[c] {
4264542647
valid++
4264642648
}
4264742649
}
42648-
42649-
for valid == len(need) { // 当窗口中已经包含了所有需要的字符时
42650-
if right-left < length { // 更新最小覆盖子串长度及起始位置
42650+
// 判断左侧窗口是否要收缩
42651+
for valid == len(need) {
42652+
/**<extend down -200>
42653+
![](../pictures/slidingwindow/2.png)
42654+
*/
42655+
// 在这里更新最小覆盖子串
42656+
if right-left < length {
4265142657
start = left
4265242658
length = right - left
4265342659
}
42660+
// d 是将移出窗口的字符
4265442661
d := s[left]
42662+
// 左移窗口
4265542663
left++
42656-
42657-
if _, ok := need[d]; ok { // 如果 d 是需要匹配的字符
42664+
// 进行窗口内数据的一系列更新
42665+
if _, ok := need[d]; ok {
4265842666
if window[d] == need[d] {
4265942667
valid--
4266042668
}
4266142669
window[d]--
4266242670
}
4266342671
}
42672+
/**<extend up -50>
42673+
![](../pictures/slidingwindow/4.png)
42674+
*/
4266442675
}
42665-
42666-
if length == math.MaxInt32 { // 如果没有符合要求的子串,返回空字符串
42676+
// 返回最小覆盖子串
42677+
if length == math.MaxInt {
4266742678
return ""
42679+
} else {
42680+
return s[start : start+length]
4266842681
}
42669-
42670-
return s[start : start+length] // 返回最小覆盖子串
4267142682
}
4267242683
```
4267342684

0 commit comments

Comments
(0)

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