牛客题霸 NC28 最小覆盖子串
生活随笔
收集整理的這篇文章主要介紹了
牛客题霸 NC28 最小覆盖子串
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
https://www.nowcoder.com/practice/c466d480d20c4c7c9d322d12ca7955ac
解決方案
Go
版本一
func minWindow(S string, T string) string {// write code hereleft, right := 0, 0 //表示窗口左右位置的指針start := 0 //start 表示最后結果字符串開始位置var minLen int = 1e9 // minlen表示最后字符串長度,二者可以表示一個字符串var need = make(map[byte]int) //need的存放字符串T的所有字符統計var window = make(map[byte]int) //window 存放現有的窗口中出現在need中的字符統計match := 0 //window與need的匹配度for _, v := range T {need[byte(v)]++}for right < len(S) {for match < len(need) && right < len(S) {c1 := S[right]if _, ok := need[c1]; ok {window[c1]++if window[c1] == need[c1] {match++}}right++}for match == len(need) {//當匹配度等于need,說明這段區間可以作為候選結果,更新retif right-left < minLen {minLen = right - leftstart = left}c2 := S[left]if _, ok := need[c2]; ok {window[c2]--if window[c2] < need[c2] {match--}}left++}}if minLen == 1e9 {return ""} else {return S[start : start+minLen]} }參考文章
總結
以上是生活随笔為你收集整理的牛客题霸 NC28 最小覆盖子串的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 牛客题霸 NC27 集合的所有子集
- 下一篇: 牛客题霸 NC29 二维数组中的查找