PART2 双指针

移动零

lc.283

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

判断子序列

lc.392

cpp 复制代码
class Solution {
public:
    bool isSubsequence(string s, string t) {
        if (s.size() == 0) {
            return true;
        }
        if (s.size() > t.size()) {
            return false;
        }
        int currS = 0, currT = 0;
        while (currT < t.size() && currS < s.size()) {
            if (s[currS] == t[currT]) {
                currS++;
            }
            currT++;
        }
        return currS == s.size() && currT <= t.size();
    }
};

盛最多水的容器

lc.11

cpp 复制代码
class Solution {
public:
    int maxArea(vector<int>& height) {
        int left = 0, right = height.size() - 1;
        int maxV = 0;
        while (left < right) {
            int w = right - left;
            int h = min(height[left], height[right]);
            maxV = max(maxV, h * w);
            if (height[left] <= height[right]) {
                left++;
            } else {
                right--;
            }
        }
        return maxV;
    }
};

K和数对的最大数目

lc.1679

cpp 复制代码
class Solution {
public:
    int maxOperations(vector<int>& nums, int k) {
        int count = 0;
        int left = 0, right = nums.size() - 1;
        sort(nums.begin(), nums.end());
        while (left < right) {
            if (nums[left] + nums[right] == k) {
                count++;
                left++;
                right--;
            } else if (nums[left] + nums[right] < k) {
                left++;
            } else {
                right--;
            }
        }
        return count;
    }
};
相关推荐
h39742 小时前
DirectShow过滤器开发-视频源过滤器
c++·windows·音视频·directshow
程序猿编码3 小时前
榨干 RTX5090 算力!Qwen3 专用单卡推理引擎,手写C++/CUDA 算子实现 MTP 推测解码
开发语言·c++·大模型推理·qwen3·多模态推理
西飘客4 小时前
vs 怎么根据dll 生成lib文件
c++·qt
程序员老陆5 小时前
深入理解 C++ thread_local:线程私有存储的正确打开方式
开发语言·c++·程序设计
信奥卷王5 小时前
[GESP202609 六级] 分树规划
算法
夏幻灵5 小时前
LeetCode 21:合并两个有序链表——双指针与哑节点详解
算法·leetcode·链表
徐小黑ACG7 小时前
Golang 基础05 结构体struct
开发语言·算法·golang
Navigator_Z7 小时前
LeetCode //C - 1286. Iterator for Combination
c语言·算法·leetcode
北极有牛8 小时前
cpp学习笔记--常量指针
java·开发语言·算法