C++ | Leetcode C++题解之第525题连续数组

题目:

题解:

cpp 复制代码
class Solution {
public:
    int findMaxLength(vector<int>& nums) {
        int maxLength = 0;
        unordered_map<int, int> mp;
        int counter = 0;
        mp[counter] = -1;
        int n = nums.size();
        for (int i = 0; i < n; i++) {
            int num = nums[i];
            if (num == 1) {
                counter++;
            } else {
                counter--;
            }
            if (mp.count(counter)) {
                int prevIndex = mp[counter];
                maxLength = max(maxLength, i - prevIndex);
            } else {
                mp[counter] = i;
            }
        }
        return maxLength;
    }
};
相关推荐
不二一8 分钟前
linux c++头文件生成源文件 使用python脚本 配置vim快捷键
linux·c++·python
飞川撸码37 分钟前
【LeetCode 热题100】17:电话号码的字母组合(详细解析)(Go语言版)
算法·leetcode·golang·dfs
蒟蒻小袁37 分钟前
力扣面试150题--从前序与中序遍历序列构造二叉树
算法·leetcode·面试
Lazy Dave41 分钟前
C++开源库argh使用教程
c++·开源·argh
奇树谦1 小时前
VTK|显示三维图像的二维切片
c++·vtk·qt5·三维显示
老歌老听老掉牙2 小时前
Open CASCADE学习|几何体切片处理:OpenMP与OSD_Parallel并行方案深度解析
c++·学习·open cascade·切片处理
superior tigre2 小时前
C++学习:六个月从基础到就业——C++20:范围(Ranges)基础
c++·学习·c++20
鸡鸭扣3 小时前
leetcode hot100:解题思路大全
数据结构·python·算法·leetcode·力扣
虾球xz3 小时前
游戏引擎学习第294天:增加手套
c++·学习·游戏引擎
龙湾开发4 小时前
计算机图形学编程(使用OpenGL和C++)(第2版)学习笔记 13.几何着色器(二)爆炸效果&修改图元类型
c++·笔记·学习·3d·图形渲染·着色器