前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【python刷题】滑动窗口法

【python刷题】滑动窗口法

作者头像
西西嘛呦
发布2021-02-05 15:47:14
8620
发布2021-02-05 15:47:14
举报
文章被收录于专栏:数据分析与挖掘

模板

代码语言:javascript
复制
left,right = 0,0
while right < len(s):
    windows.append(s[right])
    right += 1
    while (windows needs shrink):
        window.pop(0)
        left += 1

leetcode 76 最小覆盖子串

代码语言:javascript
复制
class Solution:
    def minWindow(self, s: str, t: str) -> str:
        from collections import Counter
        needs = Counter(t)
        windows = {i:0 for i in t}
        left, right = 0, 0
        valid = 0
        # 记录最小覆盖子串的起始位置以及长度
        start = 0
        leng = float("inf")
        while right < len(s):
            c = s[right]
            right += 1
            if c in needs:
                windows[c] += 1
                if needs[c] == windows[c]:
                    valid += 1
            while valid == len(needs):
                if right - left < leng:
                    start = left
                    leng = right - left
                d = s[left]
                left += 1
                if d in needs:
                    if windows[d] == needs[d]:
                        valid -= 1
                    windows[d] -= 1
        return "" if leng == float("inf") else s[start:start+leng]

leetcode 567 字符串排列

代码语言:javascript
复制
class Solution:
    def checkInclusion(self, s1: str, s2: str) -> bool:
        from collections import Counter
        needs = Counter(s1)
        windows = {i:0 for i in s1}
        left, right = 0, 0
        valid = 0
        while right < len(s2):
            c = s2[right]
            right += 1
            if c in needs:
                windows[c] += 1
                if needs[c] == windows[c]:
                    valid += 1
            while right - left > len(s1) - 1: # 窗口中只能有len(s1)个元素,
                if valid == len(needs):
                    print(s2[left:right])
                    return True
                d = s2[left]
                left += 1
                if d in needs:
                    if windows[d] == needs[d]:
                        valid -= 1
                    windows[d] -= 1
        return False

leetcode 438 找所有字母异位词

代码语言:javascript
复制
class Solution:
    def findAnagrams(self, s: str, p: str) -> List[int]:
        from collections import Counter
        needs = Counter(p)
        windows = {i:0 for i in p}
        left, right = 0, 0
        valid = 0
        res = []
        while right < len(s):
            c = s[right]
            right += 1
            if c in needs:
                windows[c] += 1
                if needs[c] == windows[c]:
                    valid += 1
            while right - left > len(p) - 1: # 窗口中只能有两个元素,
                if valid == len(needs):
                    print(s[left:right])
                    res.append(left)
                d = s[left]
                left += 1
                if d in needs:
                    if windows[d] == needs[d]:
                        valid -= 1
                    windows[d] -= 1
        return res

leetcode 3 最长无重复子串

代码语言:javascript
复制
class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        if len(s) == 0:
            return 0
        if len(s) == 1:
            return 1
        from collections import defaultdict
        windows = defaultdict(int)
        left, right = 0,0
        res = float("-inf")
        while right < len(s):
            c = s[right]
            right += 1
            windows[c] += 1
            while windows[c] > 1:
                d = s[left]
                left += 1
                windows[d] -= 1
            res = max(res, right - left)
        return res
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-02-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 模板
  • leetcode 76 最小覆盖子串
  • leetcode 567 字符串排列
  • leetcode 438 找所有字母异位词
  • leetcode 3 最长无重复子串
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档