1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| class Solution { public: int findMaxLength(vector<int>& nums) {
unordered_map<int, int> hash{{0, -1}}; int cur = 0, ans = 0; for (int i = 0; i < nums.size(); i ++) { cur += nums[i] == 0 ? -1 : 1; if (hash.count(cur)) { ans = max(ans, i - hash[cur]); } else { hash[cur] = i; } } return ans; } };
|