力扣hot100——双指针

283. 移动零

cpp 复制代码
class Solution {
public:
    void moveZeroes(vector<int>& nums) {
        for (int i = 0, j = 0; j < nums.size() || i < nums.size(); j++) {
            if (j >= nums.size()) {
                nums[i++] = 0;
                continue;
            }
            if (nums[j]) nums[i++] = nums[j];
        }
    }
};

双指针,一个指针指向要修改的位置,另一个指针遍历数组

11. 盛最多水的容器

cpp 复制代码
class Solution {
public:
    int maxArea(vector<int>& a) {
        int l = 0, r = a.size() - 1;
        int ans = 0;
        while (l < r) {
            ans = max(ans, (r - l) * min(a[l], a[r]));
            if (a[l] < a[r]) l++;
            else r--;
        }
        return ans;
    }
};

15. 三数之和

cpp 复制代码
class Solution {
public:
    struct node {
        int x, y, z;
        bool operator<(const node& t) const {
            if (x != t.x)
                return x < t.x;
            if (y != t.y)
                return y < t.y;
            if (z != t.z)
                return z < t.z;
            return false;
        }
    };
    vector<vector<int>> threeSum(vector<int>& a) {
        sort(a.begin(), a.end());
        int n = a.size();
        set<node> s;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                int t = a[i] + a[j];
                t *= -1;
                int l = i + 1, r = j - 1;
                while (l < r) {
                    int mid = (l + r + 1) / 2;
                    if (a[mid] <= t) l = mid;
                    else r = mid - 1;
                }
                if (l > i && l < j && a[l] == t)
                    s.insert({a[i], a[l], a[j]});

            }
        }
        vector<vector<int>> ans;
        for (auto [x, y, z]: s) {
            ans.push_back({x, y, z});
        }
        return ans;
    }
};

哈希,模拟

42. 接雨水

cpp 复制代码
class Solution {
public:
    int trap(vector<int>& height) {
        vector<int> a;
        a.push_back(0);
    
        int n = height.size();
        vector<int> l(n + 10, 0);
        vector<int> r(n + 10, 0);
        for (auto x : height)
            a.push_back(x);
        int ans = 0;
        
        for (int i = 1; i <= n; i++) {
            l[i] = r[i] = a[i];
        }
        stack<int> stk;

        for (int i = 1; i <= n; i++) {
            while (stk.size() && a[i] > a[stk.top()])
                stk.pop();
            if (stk.size()) {
                l[i] = l[stk.top()];
            }
            stk.push(i);
        }
        while (stk.size())
            stk.pop();

        for (int i = n; i >= 1; i--) {
            while (stk.size() && a[i] > a[stk.top()])
                stk.pop();
            if (stk.size()) {
                r[i] = r[stk.top()];
            }
            stk.push(i);
        }
        for (int i = 1; i <= n; i++) {
            int t = min(l[i], r[i]);
            if (t > a[i]) ans += t - a[i];
        }
        // return l[6];
        return ans;
    }
};

单调栈,找每根柱子左边第一个比他大的,右边第一个比他大的,那么这根柱子的贡献就是二者的最小值-它自己

相关推荐
RaymondZhao344 分钟前
【全面推导】策略梯度算法:公式、偏差方差与进化
人工智能·深度学习·算法·机器学习·chatgpt
zhangfeng113312 分钟前
DBSCAN算法详解和参数优化,基于密度的空间聚类算法,特别擅长处理不规则形状的聚类和噪声数据
算法·机器学习·聚类
圣保罗的大教堂1 小时前
leetcode 2348. 全 0 子数组的数目 中等
leetcode
啊阿狸不会拉杆1 小时前
《算法导论》第 32 章 - 字符串匹配
开发语言·c++·算法
小学生的信奥之路1 小时前
洛谷P3817题解:贪心算法解决糖果分配问题
c++·算法·贪心算法
你知道网上冲浪吗2 小时前
【原创理论】Stochastic Coupled Dyadic System (SCDS):一个用于两性关系动力学建模的随机耦合系统框架
python·算法·数学建模·数值分析
地平线开发者4 小时前
征程 6 | PTQ 精度调优辅助代码,总有你用得上的
算法·自动驾驶
Tisfy4 小时前
LeetCode 837.新 21 点:动态规划+滑动窗口
数学·算法·leetcode·动态规划·dp·滑动窗口·概率
CoovallyAIHub5 小时前
为高空安全上双保险!无人机AI护航,YOLOv5秒判安全带,守护施工生命线
深度学习·算法·计算机视觉
huangzixuan10075 小时前
08.18总结
算法·深度优先·图论