994.Contiguous Array 邻近数组
描述Given a binary array,find the maximum length of a contiguous subarray with equal number of 0 and 1. 示例Example 1: Input: [0,1] Example 2: Input: [0,1,0] Note: The length of the given binary array will not exceed 50,000. 给出二进制数组,输出连续的含有0、1个数相等的子数组的长度。 class Solution { public: int findMaxLength(vector<int>& nums) { int res = 0,n = nums.size(),sum = 0; unordered_map<int,int> m{{0,-1}}; for (int i = 0; i < n; ++i) { sum += (nums[i] == 1) ? 1 : -1; if (m.count(sum)) { res = max(res,i - m[sum]); } else { m[sum] = i; } } return res; } }; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |