Leetcode 904. Fruit Into Baskets
sliding window(滑动窗口)算法
class Solution(object):
def totalFruit(self, tree):
"""
:type tree: List[int]
:rtype: int
"""
count=collections.Counter()
left=ans=0
for i,val in enumerate(tree):
count[val]+=1
while (len(count)>=3):
count[tree[left]]-=1
if count[tree[left]]==0:
del count[tree[left]]
left+=1
ans=max(ans,i-left+1)
return ans
Leetcode 904. Fruit Into Baskets的更多相关文章
- [LeetCode] 904. Fruit Into Baskets 水果装入果篮
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your cho ...
- 【LeetCode】904. Fruit Into Baskets 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/fruit-in ...
- LeetCode - Fruit Into Baskets
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your cho ...
- [Swift]LeetCode904. 水果成篮 | Fruit Into Baskets
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your cho ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- [LeetCode] Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串
Given a string S, find the length of the longest substring T that contains at most two distinct char ...
- LeetCode Longest Substring with At Most Two Distinct Characters
原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/ 题目: Gi ...
- [LeetCode] 159. Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串
Given a string s , find the length of the longest substring t that contains at most 2 distinct char ...
随机推荐
- php会话技术之Session用法
php会话技术之Session用法举例. 本文原始链接:http://www.jbxue.com/article/8940.html1.创建session <?php //创建sessi ...
- Easyui 遮罩实现方式
项目中在提交Ajax请求时,后台处理数据时间有点长,需要一个遮罩,就随便找了一个实现一下:包含两种方式,个人比较喜欢第二种 第一种: $("#saveMaterial").clic ...
- IE浏览器连不上网,其他浏览器可以
周末因工作需要,需用IE浏览器.结果发现IE连不上网,而其他浏览器正常上网. 首先排查不是网络连接问题. 又重启了一下网络连接.禁用---->启用. 还是不好使.(最后找到原因是DNS设置问题. ...
- PAT 天梯赛 L1-006. 连续因子 【循环】
题目链接 https://www.patest.cn/contests/gplt/L1-006 思路 输出的连续因子 的乘积 也要是这个数的因子 就每个数先找它的单因子 然后每个单因子往上一个一个遍历 ...
- 服务熔断、降级、限流、异步RPC -- HyStrix
背景 伴随着业务复杂性的提高,系统的不断拆分,一个面向用户端的API,其内部的RPC调用层层嵌套,调用链条可能会非常长.这会造成以下几个问题: API接口可用性降低 引用Hystrix官方的一个例子, ...
- HTML系列(2)基本的HTML标签(一)
本节介绍基本的HTML标签的使用实例. (1)h标签: <!DOCTYPE html> <html> <head> <title>示例2 ...
- 每天一个Linux命令(51)ss命令
ss命令用来显示处于活动状态的套接字信息. (1)用法: 用法: ss [参数] ss [参数] [过滤] (2)功能: 功能: ss是类似nets ...
- 【leetcode刷题笔记】Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- Django-Ajax进阶
一.Ajax上传文件 1.form表单上传文件 文件和其他的数据类型不一样,是一个二进制的形式 Form上传文件的时候切记要加上:enctype="multipart/form-data&q ...
- Swift中的? ! as as? as!
?: 代表这是个可选类型(optional)的.如下,如果num有就为Int类型的,如果没有值那么就是nil. let num:Int? 当我对number进行显示赋值时那么number就是Int类型 ...