滑動窗口 合集

3. 無重復(fù)字符的最長子串

class Solution {
    public int lengthOfLongestSubstring(String s) {
        if (s.length()==0) return 0;
        HashMap<Character, Integer> map = new HashMap<Character, Integer>();
        int max = 0;
        int left = 0;
        for(int i = 0; i < s.length(); i ++){
            if(map.containsKey(s.charAt(i))){
                left = Math.max(left,map.get(s.charAt(i)) + 1);
            }
            map.put(s.charAt(i),i);
            max = Math.max(max,i-left+1);
        }
        return max;
        
    }
}

76.最小覆蓋子串

給你一個字符串 s 、一個字符串 t 。返回 s 中涵蓋 t 所有字符的最小子串。如果 s 中不存在涵蓋 t 所有字符的子串,則返回空字符串 ""
注意:

  • 對于 t 中重復(fù)字符,我們尋找的子字符串中該字符數(shù)量必須不少于 t 中該字符數(shù)量。
  • 如果 s 中存在這樣的子串,我們保證它是唯一的答案。
    輸入:s = "ADOBECODEBANC", t = "ABC"
    輸出:"BANC"
class Solution {
    public String minWindow(String s, String t) {
        if (s == null || s.length() == 0 || t == null || t.length() == 0){
            return "";
        }
        int[] need = new int[128];
        //記錄需要的字符的個數(shù)
        for (int i = 0; i < t.length(); i++) {
            need[t.charAt(i)]++;
        }
        //l是當前左邊界,r是當前右邊界,size記錄窗口大小,count是需求的字符個數(shù),start是最小覆蓋串開始的index
        int l = 0, r = 0, size = Integer.MAX_VALUE, count = t.length(), start = 0;
        //遍歷所有字符
        while (r < s.length()) {
            char c = s.charAt(r);
            if (need[c] > 0) {//需要字符c
                count--;
            }
            need[c]--;//把右邊的字符加入窗口
            if (count == 0) {//窗口中已經(jīng)包含所有字符
                while (l < r && need[s.charAt(l)] < 0) {
                    need[s.charAt(l)]++;//釋放右邊移動出窗口的字符
                    l++;//指針右移
                }
                if (r - l + 1 < size) {//不能右移時候挑戰(zhàn)最小窗口大小,更新最小窗口開始的start
                    size = r - l + 1;
                    start = l;//記錄下最小值時候的開始位置,最后返回覆蓋串時候會用到
                }
                //l向右移動后窗口肯定不能滿足了 重新開始循環(huán)
                need[s.charAt(l)]++;
                l++;
                count++;
            }
            r++;
        }
        return size == Integer.MAX_VALUE ? "" : s.substring(start, start + size);
    }
}

// python寫法
class Solution:
    def minWindow(self, s: 'str', t: 'str') -> 'str':
        from collections import defaultdict
        lookup = defaultdict(int)
        for c in t:
            lookup[c] += 1
        start = 0
        end = 0
        min_len = float("inf")
        counter = len(t)
        res = ""
        while end < len(s):
            if lookup[s[end]] > 0:
                counter -= 1
            lookup[s[end]] -= 1
            end += 1
            while counter == 0:
                if min_len > end - start:
                    min_len = end - start
                    res = s[start:end]
                if lookup[s[start]] == 0:
                    counter += 1
                lookup[s[start]] += 1
                start += 1
        return res

159. 至多包含兩個不同字符的最長子串

給定一個字符串 s ,找出 至多 包含兩個不同字符的最長子串 t 。
示例 1:
輸入: "eceba"
輸出: 3
解釋: t 是 "ece",長度為3。

class Solution:
    def lengthOfLongestSubstringTwoDistinct(self, s: str) -> int:
        from collections import defaultdict
        lookup = defaultdict(int)
        start = 0
        end = 0
        max_len = 0
        counter = 0
        while end < len(s):
            if lookup[s[end]] == 0:
                counter += 1
            lookup[s[end]] += 1
            end +=1
            while counter > 2:
                if lookup[s[start]] == 1:
                    counter -= 1
                lookup[s[start]] -= 1
                start += 1
            max_len = max(max_len, end - start)
        return max_len

340. 至多包含 K 個不同字符的最長子串

class Solution:
    def lengthOfLongestSubstringKDistinct(self, s: str, k: int) -> int:
        from collections import defaultdict
        lookup = defaultdict(int)
        start = 0
        end = 0
        max_len = 0
        counter = 0
        while end < len(s):
            if lookup[s[end]] == 0:
                counter += 1
            lookup[s[end]] += 1
            end += 1
            while counter > k:
                if lookup[s[start]] == 1:
                    counter -= 1
                lookup[s[start]] -= 1
                start += 1
            max_len = max(max_len, end - start)
        return max_len
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1.無重復(fù)字符的最長子串(3 - 中/劍指48) 題目描述:給定一個字符串,請你找出其中不含有重復(fù)字符的 最長子串...
    _code_x閱讀 1,488評論 2 6
  • 前言 滑動窗口類問題是面試當中的高頻題,問題本身其實并不復(fù)雜,但是實現(xiàn)起來細節(jié)思考非常的多,想著想著可能因為變量變...
    桑榆非晚95閱讀 838評論 0 0
  • 先給出labuladong框架 下面有幾道典型的窗口題,前面幾道是很容易套這個模板的。但是后面的兩道:求和和求乘積...
    奧爾良雞腿腿閱讀 578評論 0 0
  • 一 滑動窗口 滑動窗口法(sliding window)常用于輸入為數(shù)組,輸出為統(tǒng)計滿足特定約束條件的子串次數(shù)的情...
    周恩國的學(xué)習(xí)筆記閱讀 2,918評論 0 0
  • 滑動窗口算法思想是非常重要的一種思想,可以用來解決數(shù)組,字符串的子元素問題。它可以將嵌套循環(huán)的問題,轉(zhuǎn)換為單層循環(huán)...
    程序員will閱讀 1,985評論 0 1

友情鏈接更多精彩內(nèi)容