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;
    }
};
相关推荐
GalaxyPokemon几秒前
LeetCode - 704. 二分查找
数据结构·算法·leetcode
呃m1 小时前
双重特征c++
c++
景彡先生1 小时前
C++ 中文件 IO 操作详解
开发语言·c++
无影无踪的青蛙2 小时前
[C++] STL大家族之<map>(字典)容器(附洛谷)
开发语言·c++
二进制人工智能2 小时前
【OpenGL学习】(四)统一着色和插值着色
c++·opengl
红石程序员3 小时前
VSCode配置C++项目全攻略
开发语言·c++·visual studio
liuqun03194 小时前
开心灿烂go开发面试题
算法·leetcode·golang
liulilittle4 小时前
通过高级处理器硬件指令集AES-NI实现AES-256-CFB算法并通过OPENSSL加密验证算法正确性。
linux·服务器·c++·算法·安全·加密·openssl
এ᭄画画的北北4 小时前
力扣-279.完全平方数
数据结构·算法·leetcode
十秒耿直拆包选手5 小时前
Qt:Qt桌面程序正常退出注意事项
c++·qt