Reprinted: Sliding window template

def findSubArray(nums):
    N = len(nums) # 数组/字符串长度
    left, right = 0, 0 # 双指针,表示当前遍历的区间[left, right],闭区间
    sums = 0 # 用于统计 子数组/子区间 是否有效,根据题目可能会改成求和/计数
    res = 0 # 保存最大的满足题目要求的 子数组/子串 长度
    while right < N: # 当右边的指针没有搜索到 数组/字符串 的结尾
        sums += nums[right] # 增加当前右边指针的数字/字符的求和/计数
        while 区间[left, right]不符合题意: # 此时需要一直移动左指针,直至找到一个符合题意的区间
            sums -= nums[left] # 移动左指针前需要从counter中减少left位置字符的求和/计数
            left += 1 # 真正的移动左指针,注意不能跟上面一行代码写反
        # 到 while 结束时,我们找到了一个符合题意要求的 子数组/子串
        res = max(res, right - left + 1) # 需要更新结果
        right += 1 # 移动右指针,去探索新的区间
    return res

作者:负雪明烛
链接:https://leetcode.cn/problems/max-consecutive-ones-iii/solutions/609055/fen-xiang-hua-dong-chuang-kou-mo-ban-mia-f76z/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

The left and right pointers are used in the sliding window, and the idea of ​​their movement is: the right pointer is used as the drive, and the left pointer is dragged forward. The right pointer moves only one step at a time, while the left pointer may move multiple steps at a time in the inner while loop. The right pointer is actively moving forward to explore new unknown areas; the left pointer is forced to move and is responsible for finding an interval that satisfies the meaning of the question.

The overall idea of ​​the template is:

    Define two pointers, left and right, to point to the beginning and end of the interval respectively, note that it is a closed interval; define sums to count the number of occurrences of each character in the interval; the first while loop is to judge whether the position of the right pointer exceeds the
    array Boundary; when right reaches a new position every time, the sum/count of the right pointer needs to be increased; the
    second while loop is to move the left pointer to the right to a position in the [left, right] interval that meets the meaning of the question; when left moves each time At the new position, the sum/count of the left pointer needs to be reduced;
    after the second while loop, a [left, right] interval that meets the meaning of the question is successfully found, and the question requires the largest interval length, so update res to max( res, the length of the current interval).
    The right pointer moves one step to the right each time to start exploring a new interval.

The sums in the template need to be modified according to the meaning of the title. This question is a summing question, so sums is defined as an integer for summing; if it is a counting question, it needs to be changed to a dictionary for counting. When the left and right pointers change, sums needs to be updated.

Another thing that needs to be modified according to the topic is the judgment condition of the inner while loop, that is: the interval [left, right] does not meet the meaning of the topic.

Author: Negative Xue Mingzhu
Link: https://leetcode.cn/problems/max-consecutive-ones-iii/solutions/609055/fen-xiang-hua-dong-chuang-kou-mo-ban-mia-f76z/
Source: LeetCode
copyright belongs to the author. For commercial reprint, please contact the author for authorization, for non-commercial reprint, please indicate the source.

Guess you like

Origin blog.csdn.net/weixin_45314061/article/details/130449789