首页 > 其他 > 详细

[leetcode]Fruit Into Baskets

时间:2020-02-07 12:40:29      阅读:51      评论:0      收藏:0      [点我收藏+]

滑动窗口,原来可以通过循环嵌套循环的方式实现。

class Solution:
    def totalFruit(self, tree: List[int]) -> int:

        result = 0
        fruitDict = {}
        i = 0
        # assert i <= j
        for j in range(len(tree)):
            x = tree[j]
            if x not in fruitDict:
                fruitDict[x] = 0
            fruitDict[x] += 1
            while len(fruitDict) == 3:
                fruitDict[tree[i]] -= 1
                if fruitDict[tree[i]] == 0:
                    del fruitDict[tree[i]]
                i += 1
            result = max(result, j - i + 1)

        return result
        
                
                

  

[leetcode]Fruit Into Baskets

原文:https://www.cnblogs.com/lautsie/p/12271637.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!