class Solution {
public:
int totalFruit(vector<int>& tree) {
int res = 0;
unordered_map<int, int> cnt;
for (int i = 0, j = 0, s = 0; i < tree.size(); i ++ ) {
if ( ++ cnt[tree[i]] == 1) s ++ ;
while (s > 2) {
if ( -- cnt[tree[j]] == 0) s -- ;
j ++ ;
}
res = max(res, i - j + 1);
}
return res;
}
};
标签:cnt,904,++,res,tree,int,成篮,LeetCode
From: https://www.cnblogs.com/zjy0/p/16798931.html