给定一个二进制数组, 找到含有相同数量的 0 和 1 的最长连续子数组(的长度)。 示例 1:输入: [0,1]输出: 2说明: [0, 1] 是具有相同数量0和1的最长连续子数组。示例 2:输入: [0,1,0]输出: 2说明: [0, 1] (或 [1, 0]) 是具有相同数量0和1的最长连续子数组。算法:哈希表,线性扫描。
class Solution {
public:
int findMaxLength(vector<
int>&
nums) {
int n=nums.size(),ans=
0,x=
0;
unordered_map<
int,
int>
hash;
hash[0]=
0;
for(
int i=
0;i<n;i++
){
if(!nums[i])x--
;
else x++
;
if(hash.find(x)!=
hash.end())
ans=max(ans,i+
1-
hash[x]);
else
hash[x]=i+
1;
}
return ans;
}
};
转载于:https://www.cnblogs.com/programyang/p/11157858.html
相关资源:各显卡算力对照表!