LeetCode专项练习之滑动窗口(Sliding window)笔记

本文是根据穷码农的LeetCode刷题建议而进行专项练习时记录的心得。

滑动窗口,笼统来讲就是在列表(数组)里建立一个滑动窗口,并往其中塞入满足条件的元素。每循环一次,右指针便会右移,同时依据情况移动左指针。

今天的笔记包含滑动窗口(Sliding Window)类型下的7个题目,它们在leetcode上的编号和题名分别是:

  • 3 - Longest Substring Without Repeating Characters
  • 30 - Substring with Concatenation of All Words
  • 76 - Minimum Window Substring
  • 209 - Minimum Size Subarray Sum 
  • 239 - Sliding Window Maximum
  • 424 - Longest Repeating Character Replacement
  • 904 - Fruit into Baskets

下面将根据以上顺序分别记录代码和对应心得,使用的编译器为Python3。


Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters.

Example 1:
Input: "abcabcbb"
Output: 3 
Explanation: The answer is "abc", with the length of 3. 

Example 2:
Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.

Example 3:
Input: "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3. 

Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

此题的关键是使用队列。在新增遇到重复字符前,记录并更新最长长度,遇到时队列另一测就开始出队,直到遇到重复字符为止(重复字符串也得出队)。个人认为难度较低。

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        # solution: 队列 (滑动窗口)
        result = []
        max = 0
        for element in s:
            if len(result) == 0 or element not in result:
                result.append(element)
                cur = len(result)
                if cur > max:
                    max = cur
            else:
                while result[0] != element:

                    result.remove(result[0])

                result.remove(element)
                result.append(element)

        return max

 

Substring with Concatenation of All Words

You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.

Example 1:
Input:
  s = "barfoothefoobarman",
  words = ["foo","bar"]
Output: [0,9]
Explanation: Substrings starting at index 0 and 9 are "barfoo" and "foobar" respectively.
The output order does not matter, returning [9,0] is fine too.

Example 2:
Input:
  s = "wordgoodgoodgoodbestword",
  words = ["word","good","best","word"]
Output: []

此题虽然没有被列入为穷码农的滑动窗口推荐题目,但考虑到它的热度相对较大,我便选择它来进行练习。并且,此题采用了原创解法,虽然效率较低,但起码对于一道hard题,能自己写出来也算是一种进步吧。其核心思想是利用双指针,比对字符串中每个潜在单词是否属于规定单词列表里的单词,若满足条件则用字典进行记录,并在每次循环时比对该字典里的所有单词是否与单词列表完全匹配。值得注意的是,如果字典最终无法完全匹配,那便清除字典,并重新记录。但此时左指针的起点将为原字典里第一个记录的单词的第二个字母,而非第二个记录的单词。

from collections import Counter

class Solution:
    def findSubstring(self, s: str, words: list) -> list:
        # solution: sliding window。原创做法,但效率较低。

        # special considerations:
        if len(s) == 0 or len(words) == 0:
            return []

        # create parameters
        ans = []
        wordsLen = len(words[0])
        wordform = Counter(words)
        left, right = 0, wordsLen-1
        curWordDic = {}
        mark = -1

        # start moving the right index
        while right < len(
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值