Sliding Window算法题总结(持续更新)

438. Find All Anagrams in a String

时间复杂度O(N)
滑动窗口的基本模板:
1.map存目标串的字符,设匹配长度为目标串长度
2.设置左右指针,右指针遍历原字符串,如果碰到目标串的字符,匹配长度-1
3.如果左右指针夹着的串的长度等于目标串长度,检查两串是否满足要求
4.判断完成,左指针开始扫,如果扫到目标串的字符,恢复匹配长度,匹配长度+1

class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        
        vector<int>res;
        //异常处理
        if(s.empty()||p.empty()||s.size()<p.size()){
            return res;
        }
        
        //map 用来记录字符的数目
        vector<int>mp(256,0);
        //左右指针,用于遍历字符串s
        int begin = 0;
        int end = 0;
        for(char c:p){
            mp[c-'a']++;
        }
        
        //s字符串左右指针夹着的子串与目标串如果是满足条件的,matchSize=0,初始化为目标串的大小
        int matchSize = p.size();
        int sSize = s.size();
        while(end < sSize) {
            char cend = s[end];
            //如果扫到了目标串含有的字符,map[char]--
            if(mp[cend-'a']>0){
                matchSize--;
            }
            mp[cend-'a']--;
            
            if(end-begin==p.size()-1) {
                
                if(matchSize==0){
                    res.push_back(begin);
                }
                char cbegin = s[begin];
                //如果是不在目标串的字符,那么结果肯定是负数,否则为0
                //所以如果>=0,那么肯定扫到的是目标串内的字符
                //进行匹配size的复原
                if(mp[cbegin-'a']>=0){
                    matchSize++;
                }
                mp[cbegin-'a']++;
                begin++;
            }
            end++;
        }    
        return res;
    }
};

76. Minimum Window Substring

Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).

Example:

Input: S = “ADOBECODEBANC”, T = “ABC”
Output: “BANC”

class Solution {
public:
    string minWindow(string s, string t) {
     
        string ans;
        
        if(s.empty()||t.empty()||s.size()<t.size()){
            return ans;   
        }
        
        vector<int>mp(256,0);
        for(char c: t){
            mp[c]++;
        }
        int begin = 0;
        int end = 0;
        int matchSize = t.size();
        int sSize = s.size();
        int minLen = INT_MAX;
        int resultIndex = 0;
        while(end<sSize){
            char cend = s[end];
            if(mp[cend]>0){
                matchSize--;
            }
            mp[cend]--;
            end++;
            while(matchSize==0){
                if(end-begin<minLen){
                    minLen = end-begin;
                    resultIndex = begin;
                }
                char cbegin = s[begin];
                //这时候窗口就不符合要求了,下个循环会退出
                if(mp[cbegin]==0){
                    matchSize++;
                }
                mp[cbegin]++;
                begin++;
            }
        }
        return minLen==INT_MAX?"":s.substr(resultIndex,minLen);
    }
};

3 Longest substring without repetitive characters

时间复杂度(O(n))
常规的左右指针,右指针扫,如果扫到mp[s[end]]>1,说明扫到重复了,然后begin指针开始干活,一直扫,便扫mp[s[begin]]边减1,一直到mp[s[end]]等于1。然后每次end移动比较最大长度

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int ans = 0;
        if(s.empty()){
            return 0;
        }
        int begin = 0;
        int end = 0;
        int strSize = s.size();
        vector<int>mp(256,0);
        while(end<strSize){
            mp[s[end]]++;
            while(mp[s[end]]>1){
                mp[s[begin]]--;
                begin++;
            }
            ans = max(ans,end-begin+1);
            end++;
        }
        return ans;
    }
};

209 Minimum Size Subarray Sum

Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn’t one, return 0 instead.

Example:

Input: s = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: the subarray [4,3] has the minimal length under the problem constraint.

Follow up:

If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log n).

双指针滑动窗口

class Solution {
public:
    int minSubArrayLen(int s, vector<int>& nums) {
        int ans = INT_MAX;
        int size = nums.size();
        int begin = 0;
        int end = 0;
        int sum = 0;
        while(end<size){
            
            while(end<size && sum <s) {
                sum+=nums[end];
                end++;
            }
            
            //可以提前结束
            if(sum<s){
                break;
            }
            
            while(begin<end&&sum>=s){
                ans = min(ans,end-begin);
                sum-=nums[begin];
                begin++;
            }
            
        }
        return ans==INT_MAX?0:ans;
    }
};

239

.Input: nums = [1,3,-1,-3,5,3,6,7], and k = 3
Output: [3,3,5,5,6,7]
Explanation:

Window position Max


[1 3 -1] -3 5 3 6 7 3
1 [3 -1 -3] 5 3 6 7 3
1 3 [-1 -3 5] 3 6 7 5
1 3 -1 [-3 5 3] 6 7 5
1 3 -1 -3 [5 3 6] 7 6
1 3 -1 -3 5 [3 6 7] 7
Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. Return the max sliding window.

做法一: multiset

multiset用类似红黑树的数据结构内置帮数据进行排序
时间复杂度O(nlogn)

class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        
        multiset<int>st;
        vector<int>vec;
        int size = nums.size();
        for(int i=0;i<size;i++) {
            
            //把最左边元素删掉,erase()应该提供一个确切的iterator位置
            if(i-k>=0){
                st.erase(st.find(nums[i-k]));
            }
            st.insert(nums[i]);
            //multiset默认从小到大升序
            if(i>=k-1){
                vec.push_back(*st.rbegin());
            }
        }
        return vec;
    }
};

做法二:deque

deque维护一个递减序列的元素的下标,时刻保证deque第一个元素是最大的.
当i-第一个元素的下标>=k,意味着窗口向右推进了一步
通过while循环来维护deque单调递减

class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        
        deque<int>dq;
        vector<int>res;
        
        for(int i=0;i<nums.size();i++){
            
            if(!dq.empty()&&i-dq.front()>=k){
                dq.pop_front();
            }
            while(!dq.empty()&&nums[dq.back()]<nums[i]){
                dq.pop_back();
            }
            dq.push_back(i);
            if(i-k>=-1){
                res.push_back(nums[dq.front()]);
            }
        }
        return res;
        
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值