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;
    }
};
相关推荐
蹊黎1 小时前
C++中vector类的部分简单实现
开发语言·c++·visual studio
Navigator_Z7 小时前
LeetCode //C - 1289. Minimum Falling Path Sum II
c语言·算法·leetcode
mmmmath_38 小时前
LeetCode.104.二叉树的最大深度
数据结构·leetcode
xier_ran8 小时前
【infra之路】AI 编译器学习笔记(六):重拾 C++——lambda 与 MLIR 中的回调
c++·笔记·学习
jimy110 小时前
c++的 Type Traits (一)
开发语言·c++
别动我齐刘海11 小时前
Linux 系统调试学习2
linux·运维·c++·人工智能·学习·机器学习·机器人
ZZHow102412 小时前
Ascend C算子开发(高级)笔记
c++·人工智能·笔记·深度学习·华为云
Tisfy12 小时前
LeetCode 2333.最小差值平方和:排序 + 数学计算(不用二分)
数学·算法·leetcode·题解·贪心·排序
aqiu11111112 小时前
【C++算法打怪专栏】LeetCode 217. 存在重复元素(解法总结)
c++·算法·leetcode
lojox13 小时前
深入理解 C++ STL:vector 底层实现、迭代器失效与 list 特性
开发语言·c++