力扣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;
    }
};

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

相关推荐
YuTaoShao1 小时前
【LeetCode 热题 100】56. 合并区间——排序+遍历
java·算法·leetcode·职场和发展
二进制person5 小时前
Java SE--方法的使用
java·开发语言·算法
OneQ6665 小时前
C++讲解---创建日期类
开发语言·c++·算法
JoJo_Way5 小时前
LeetCode三数之和-js题解
javascript·算法·leetcode
.30-06Springfield6 小时前
人工智能概念之七:集成学习思想(Bagging、Boosting、Stacking)
人工智能·算法·机器学习·集成学习
凌肖战8 小时前
力扣网C语言编程题:在数组中查找目标值位置之二分查找法
c语言·算法·leetcode
weixin_478689768 小时前
十大排序算法汇总
java·算法·排序算法
luofeiju9 小时前
使用LU分解求解线性方程组
线性代数·算法
学不动CV了9 小时前
数据结构---线性表理解(一)
数据结构
SKYDROID云卓小助手9 小时前
无人设备遥控器之自动调整编码技术篇
人工智能·嵌入式硬件·算法·自动化·信号处理