leetcode525连续数组

 

class Solution {
public:
    int findMaxLength(vector<int>& nums) {
        int Max_length = 0;
        unordered_map<int,int> MP;
        int counter = 0;
        MP[counter] = -1;
        for(int i  = 0;i<nums.size();i++)
        {
            int num = nums[i];
            if(num == 1)
            {
                counter++;
            }
            else
            {
                counter--;
            }
            if(MP.count(counter))
            {
                int current_id  = MP[counter];
                Max_length = max(Max_length,i-current_id);
            }
            else{
                MP[counter] = i;
            }
        }
        return Max_length;
    }
};

 

 

posted @ 2021-10-26 17:54  A-inspire  Views(17)  Comments(0Edit  收藏  举报