leetcodeD3

今天学习双向指针滑动窗口【基础算法精讲 03】_哔哩哔哩_bilibili

209. 长度最小的子数组 - 力扣(LeetCode)

cpp 复制代码
class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        // 滑动窗口的核心思想是 "扩大右边界累加,满足条件时收缩左边界"
        int n = nums.size();
        int left = 0, right = 0;
        int ans = n + 1;
        int sum = 0;
        for (right; right < n; right++) {
            sum += nums[right];
            while (sum >= target) {
                ans = min(ans, right - left + 1);
                sum -= nums[left];
                left++;
            }
        }
        return ans > n ? 0 : ans;
    }
};

这道题当作模板背过。

713. 乘积小于 K 的子数组 - 力扣(LeetCode)

cpp 复制代码
class Solution {
public:
    int numSubarrayProductLessThanK(vector<int>& nums, int k) {
        // 子数组内所有元素的乘积严格小于 k 的连续子数组的数目
        // 边界条件:k≤1时,无满足条件的子数组(数组元素为正整数)
        if (k <= 1) return 0;
        
        int n = nums.size();
        int left = 0, right = 0;
        int ans = 0;
        long long product = 1; // 用long long避免整数溢出
        
        for (; right < n; right++) {
            product *= nums[right]; // 右边界扩窗,累乘
            
            // 乘积≥k时,收缩左边界直到乘积<k
            while (product >= k) {
                product /= nums[left];
                left++;
            }
            
            // 以right为结尾的满足条件的子数组数目 = 窗口长度
            ans += right - left + 1;
        }
        
        return ans;
    }
};

总结

cpp 复制代码
1.边界条件处理(k≤1):
题目中 nums 的元素都是正整数,所以任何子数组的乘积≥1。如果 k≤1,没有子数组能满足 "乘积严格小于 k",直接返回 0,避免后续错误计算。
2.避免整数溢出:
将product的类型从int改为long long,因为当 nums 的元素较大、数组较长时,累乘的结果会超出 int 的范围(比如 nums=[10,2,3], k=1000,累乘到 1023=60 没问题,但更长的数组会溢出)。

3. 无重复字符的最长子串 - 力扣(LeetCode)

cpp 复制代码
class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int ans=0;
        int left=0;int right=0;
        int n=s.length();
        unordered_set<char> window;
        for(right;right<n;right++){
            char c=s[right];
            while(window.contains(c)){
                window.erase(s[left]);
                left++;
            }
            window.insert(c);
            ans=max(ans,right-left+1);
        }
        return ans;
    }
};

哈希常用

cpp 复制代码
unordered_set<char> window;
window.contains(c))
window.erase(s[left]);
window.insert(c);
相关推荐
踩坑记录19 小时前
leetcode hot100 easy 101. 对称二叉树 递归 层序遍历 bfs
算法·leetcode·宽度优先
2501_9403152620 小时前
leetcode182动态口令(将字符的前几个元素放在字符串后面)
算法
老鼠只爱大米20 小时前
LeetCode经典算法面试题 #98:验证二叉搜索树(递归法、迭代法等五种实现方案详解)
算法·leetcode·二叉树·递归·二叉搜索树·迭代
疯狂的喵1 天前
C++编译期多态实现
开发语言·c++·算法
scx201310041 天前
20260129LCA总结
算法·深度优先·图论
2301_765703141 天前
C++中的协程编程
开发语言·c++·算法
m0_748708051 天前
实时数据压缩库
开发语言·c++·算法
小魏每天都学习1 天前
【算法——c/c++]
c语言·c++·算法
智码未来学堂1 天前
探秘 C 语言算法之枚举:解锁解题新思路
c语言·数据结构·算法
Halo_tjn1 天前
基于封装的专项 知识点
java·前端·python·算法