程式語言 - LeetCode - C++ - 525. Contiguous Array



題目:


解答:

class Solution {
public:
    int findMaxLength(vector<int>& nums) {
        int ans = 0;
        int prefix = 0;
        int n = nums.size();
        unordered_map<int, int> mp;

        mp[0] = -1;
        for (int i = 0; i < n; ++i) {
            if (nums[i] == 0) {
                prefix -= 1;
            }
            else {
                prefix += 1;
            }

            if (mp.count(prefix)) {
                ans = max(ans, i - mp[prefix]);
            }
            else {
                mp[prefix] = i;
            }
        }

        return ans;
    }
};