LeetCode "525. Contiguous Array" !

A variation of 'maximum sum of subarray' - using hashmap

class Solution {
public:
    int findMaxLength(vector<int>& nums) {
        int c0 = 0, c1 = 0;
        int r = 0;
        unordered_map<int, int> hm;
        for(int i = 0; i < nums.size(); i ++)
        {
            c0 += !nums[i];
            c1 += nums[i];
            int d = c0 - c1;
            if(d == 0)
            {
                r = max(r, i + 1);
            }
            else
            {
                bool bFound = hm.find(-d) != hm.end();
                if(bFound) r = max(r, i + 1 - hm[-d]);    
                else hm[-d] = i + 1;
            }
        }
        return r;
    }
};
原文地址:https://www.cnblogs.com/tonix/p/6472951.html