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;
    }
};
相关推荐
圣保罗的大教堂2 小时前
leetcode 3622. 判断整除性 简单
leetcode
_Narcissus_2 小时前
链表算法题和静态链表
数据结构·c++·笔记·算法·链表·ai·力扣
布莱克6052 小时前
C++拷贝构造与拷贝赋值运算符的区别详解
开发语言·c++
Lyyaoo.2 小时前
【普通数组】【中等】除了自身以外数组的乘积
数据结构·算法·leetcode
啦啦啦啦啦zzzz3 小时前
时间轮定时器
linux·服务器·网络·c++·定时器
圣保罗的大教堂3 小时前
leetcode 2996. 大于等于顺序前缀和的最小缺失整数 简单
leetcode
此生决int4 小时前
深入理解C++系列(15)——AVL树
开发语言·c++
一只旭宝4 小时前
预约系统版本2(pyhton+flask可视化版本)
服务器·数据库·c++·笔记·python·flask
圣保罗的大教堂4 小时前
leetcode 3702. 按位异或非零的最长子序列 中等
leetcode
爱奥尼欧4 小时前
10.C++ string 实现详解
java·开发语言·c++