3-7/3-8滑动窗口

news/2024/5/20 9:06:20 标签: 滑动窗口

 

LeetCode209 长度最小的子数组 medium

方法1:暴力法

class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        //暴力法
        int n=nums.size();
        int sum=0;
        int ret=n+1;  
        for(int i=0;i<n;i++)
        {
            sum=0;
            for(int j=i;j<n;j++)
            {
                sum+=nums[j];
                if(sum>=target)
                {
                    ret=min(ret,j-i+1);
                    break;
                }
            }
        }
        if(ret==n+1)
            return 0;
        return ret;
    }
};

方法2:前缀和+二分查找

class Solution {
public:
    int minSubArrayLen(int s, vector<int>& nums) {
        int n = nums.size();
        if (n == 0) {
            return 0;
        }
        int ans = INT_MAX;
        vector<int> sums(n + 1, 0); 
        // 为了方便计算,令 size = n + 1 
        // sums[0] = 0 意味着前 0 个元素的前缀和为 0
        // sums[1] = A[0] 前 1 个元素的前缀和为 A[0]
        // 以此类推
        for (int i = 1; i <= n; i++) {
            sums[i] = sums[i - 1] + nums[i - 1];
        }
        for (int i = 1; i <= n; i++) {
            int target = s + sums[i - 1];
            auto bound = lower_bound(sums.begin(), sums.end(), target);
            if (bound != sums.end()) {
                ans = min(ans, static_cast<int>((bound - sums.begin()) - (i - 1)));
            }
        }
        return ans == INT_MAX ? 0 : ans;
    }
};

方法3.滑动窗口

class Solution {
public:
    int minSubArrayLen(int s, vector<int>& nums) {
        int result = INT32_MAX;
        int sum = 0; // 滑动窗口数值之和
        int i = 0; // 滑动窗口起始位置
        int subLength = 0; // 滑动窗口的长度
        for (int j = 0; j < nums.size(); j++) {
            sum += nums[j];
            // 注意这里使用while,每次更新 i(起始位置),并不断比较子序列是否符合条件
            while (sum >= s) {
                subLength = (j - i + 1); // 取子序列的长度
                result = result < subLength ? result : subLength;
                sum -= nums[i++]; // 这里体现出滑动窗口的精髓之处,不断变更i(子序列的起始位置)
            }
        }
        // 如果result没有被赋值的话,就返回0,说明没有符合条件的子序列
        return result == INT32_MAX ? 0 : result;
    }
};

 

LeetCode3 medium

方法:滑动窗口

当窗口内没有重复字母时,调整右边界

当窗口内出现重复字母时,调整左边界

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        if(s.size() == 0) return 0;
        unordered_set<char> lookup;
        int maxStr = 0;
        int left = 0;
        for(int i = 0; i < s.size(); i++){
            while (lookup.find(s[i]) != lookup.end()){
                lookup.erase(s[left]);
                left ++;
            }
            maxStr = max(maxStr,i-left+1);
            lookup.insert(s[i]);
    }
        return maxStr;
        
    }
};
    int lengthOfLongestSubstring(string s) {
        vector<int> m(128, 0);
        int ans = 0;
        int i = 0;
        for (int j = 0; j < s.size(); j++) {
            i = max(i, m[s[j]]);
            m[s[j]] = j + 1;
            ans = max(ans, j - i + 1);
        }
        return ans;
    }

LeetCode438 medium

 

LeetCode76 hard

LeetCode30 hard

LeetCode159 medium

LeetCode239 hard

LeetCode340 medium

LeetCode567 medium

LeetCode632 hard

LeetCode727 hard

 


http://www.niftyadmin.cn/n/1032285.html

相关文章

4-1/4-3set和map的使用

目录 LeetCode349 easy LeetCode350 easy LeetCode242 easy LeetCode202 easy LeetCode290 easy LeetCode205 easy LeetCode451 medium LeetCode349 easy 方法1&#xff1a;使用set class Solution { public:vector<int> intersection(vector<int>& n…

4-4查找表

目录 LeetCode1 easy LeetCode15 medium LeetCode18 medium ​​​​LeetCode16 medium LeetCode1 easy 方法1&#xff1a;暴力 class Solution { public:vector<int> twoSum(vector<int>& nums, int target) {vector<int> res;for(int i0;i<nu…

4-5

LeetCode454 class Solution { public:int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {int ans0;unordered_map<int,int> um; //设置查找表for(int i0;i<C.size();i){for(int j0;j<…

LeetCode 146 LRU缓存机制

方法1&#xff1a;利用C自带的双向链表list和散列表unordered_map class LRUCache { public:LRUCache(int capacity) : cap(capacity) { //构造函数}int get(int key) { if (map.find(key) map.end()) return -1; //找不到&#xff0c;返回-1 &#xff08;使用哈希表提…

LeetCode460 LFU缓存

struct Node { //双向链表的节点int key; //键int val; //值int freq; //频率Node* prev; //前一个节点Node* next; //后一个节点//无参构造函数Node () : key(-1), val(-1), freq(0), prev(nullptr), next(nullptr) {}//带参构造函数Node (int _k, int _v) : key(_k…

动态规划II

目录 力扣343 整数拆分 方法1&#xff1a;动态规划 力扣279.完全平方数 方法1&#xff1a;动态规划 力扣343 整数拆分 方法1&#xff1a;动态规划 class Solution { public: int max3(int x,int y,int z) {return max(x,max(y,z));//return x>y?x:(y>z?y:z); /…

剑指offer 09 两个栈实现队列

思路&#xff1a; 入队时&#xff0c;直接进到A中 出队时&#xff0c;看B是否为空&#xff0c;若不是空&#xff0c;直接从B弹出结果 否则&#xff0c;看A是否为空&#xff0c;如果为空&#xff0c;说明目前没有元素&#xff0c;返回-1 否则&#xff0c;将A中的元素逐个出栈…

树相关

目录 LeetCode104.二叉树的最大深度 方法1&#xff1a;递归 方法2&#xff1a;层序遍历 LeetCode 559.N叉树的最大深度 方法1&#xff1a;递归 方法2&#xff1a;层序遍历 剑指offer 55-II 平衡二叉树 方法1&#xff1a;递归 LeetCode104.二叉树的最大深度 二叉树的最…