首页 > 其他分享 >LeetCode 904. 水果成篮

LeetCode 904. 水果成篮

时间:2022-10-17 13:57:58浏览次数:35  
标签:cnt 904 ++ res tree int 成篮 LeetCode

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

相关文章