leetcode525连续数组

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;
    }
};

 

 

leetcode525连续数组

上一篇:vue


下一篇:Go并发编程之美-CAS操作