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

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

相关推荐
长安er5 分钟前
LeetCode 46/51 排列型回溯题笔记-全排列 / N 皇后
笔记·算法·leetcode·回溯·递归·n皇后
天赐学c语言5 分钟前
12.16 - 全排列 && C语言中声明和定义的区别
c++·算法·leecode
LYFlied6 分钟前
【每日算法】LeetCode 146. LRU 缓存机制
前端·数据结构·算法·leetcode·缓存
沟通QQ87622396511 分钟前
PLL锁相环程序+MATLAB仿真文件。 (SOGI+DQ)程序用stm32G431芯片写的(...
贪心算法
a努力。14 分钟前
小红书Java面试被问:ThreadLocal 内存泄漏问题及解决方案
java·jvm·后端·算法·面试·架构
LYFlied20 分钟前
【每日算法】LeetCode142. 环形链表 II
数据结构·算法·leetcode·链表
超级大只老咪21 分钟前
“和”与“或”逻辑判断与条件取反(Java)
java·算法
LYFlied22 分钟前
【每日算法】LeetCode 23. 合并 K 个升序链表
前端·数据结构·算法·leetcode·链表
xiaoxue..23 分钟前
LeetCode 第 15 题:三数之和
前端·javascript·算法·leetcode·面试
yaoh.wang25 分钟前
力扣(LeetCode) 28: 找出字符串中第一个匹配项的下标 - 解法思
python·程序人生·算法·leetcode·面试·职场和发展·跳槽