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;
    }
};
相关推荐
Jerry16 分钟前
LeetCode 226. 翻转二叉树
算法
想做小南娘,发现自己是女生喵1 小时前
【无标题】
数据结构·算法
Kx_Triumphs3 小时前
HDU4348 To the moon(主席树区间修改模板)
算法·题解
旖-旎3 小时前
《LeetCode647 回文子串 || LeetCode 5 最长回文子串》
c++·算法·leetcode·动态规划·哈希算法
Darkwanderor4 小时前
对Linux的进程控制的研究
linux·运维·c++
云泽8084 小时前
从零吃透 C++ 异常:抛出捕获、栈展开、异常重抛与编码规范详解
开发语言·c++·代码规范
轻颂呀4 小时前
约瑟夫环问题
算法
REDcker5 小时前
libdatachannel 快速入门
c++·webrtc·datachannel
科技大视界6 小时前
投资AI项目,传统尽调不够用了——李章虎律师拆解算法、数据、算力三大雷区
人工智能·算法·数据挖掘