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;
    }
};
相关推荐
励志不掉头发的内向程序员15 分钟前
STL库——string(类模拟实现)
开发语言·c++
郝学胜-神的一滴42 分钟前
使用C++11改进工厂方法模式:支持运行时配置的增强实现
开发语言·c++·程序人生·设计模式
Korloa1 小时前
表达式(CSP-J 2021-Expr)题目详解
c语言·开发语言·数据结构·c++·算法·蓝桥杯·个人开发
yodala2 小时前
C++中的内存管理(二)
开发语言·c++
农场主John2 小时前
(栈)Leetcode155最小栈+739每日温度
windows·python·算法·leetcode·
艾莉丝努力练剑3 小时前
【C语言16天强化训练】从基础入门到进阶:Day 5
c语言·c++·学习·算法
杜子不疼.5 小时前
【LeetCode 415】—字符串相加算法详解
算法·leetcode·职场和发展
仙俊红5 小时前
LeetCode每日一题,2025-08-21
算法·leetcode·职场和发展
重启的码农5 小时前
llama.cpp 分布式推理介绍(5) RPC 通信协议
c++·人工智能·神经网络
liulilittle5 小时前
UTF-8 编解码可视化分析
c++·字符串·unicode·string·字符·char·utf8