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;
    }
};
相关推荐
南境十里·墨染春水1 天前
C++传记(面向对象)虚析构函数 纯虚函数 抽象类 final、override关键字
开发语言·c++·笔记·算法
2301_797172751 天前
基于C++的游戏引擎开发
开发语言·c++·算法
有为少年1 天前
告别“唯语料论”:用合成抽象数据为大模型开智
人工智能·深度学习·神经网络·算法·机器学习·大模型·预训练
比昨天多敲两行1 天前
C++ 二叉搜索树
开发语言·c++·算法
Season4501 天前
C++11之正则表达式使用指南--[正则表达式介绍]|[regex的常用函数等介绍]
c++·算法·正则表达式
Tisfy1 天前
LeetCode 2839.判断通过操作能否让字符串相等 I:if-else(两两判断)
算法·leetcode·字符串·题解
问好眼1 天前
《算法竞赛进阶指南》0x04 二分-1.最佳牛围栏
数据结构·c++·算法·二分·信息学奥赛
海海不瞌睡(捏捏王子)1 天前
C++ 知识点概要
开发语言·c++
会编程的土豆1 天前
【数据结构与算法】优先队列
数据结构·算法
minji...1 天前
Linux 进程信号(二)信号的保存,sigset_t,sigprocmask,sigpending
linux·运维·服务器·网络·数据结构·c++·算法