边工作边刷题:70天一遍leetcode: day 71
Longest Substring with At Most Two Distinct Characters
# Given a string, find the length of the longest substring T that contains at most 2 distinct characters.
# For example, Given s = “eceba”,
# T is "ece" which its length is 3.
# Hide Company Tags Google
# Hide Tags Hash Table Two Pointers String
# Hide Similar Problems (M) Longest Substring Without Repeating Characters (H) Sliding Window Maximum (H) Longest Substring with At Most K Distinct Characters
class Solution(object):
def lengthOfLongestSubstringTwoDistinct(self, s):
"""
:type s: str
:rtype: int
"""
char1, right1, char2, right2 = '', 0, '', 0
start = 0
longest = 0
for i, c in enumerate(s):
if not char1 or char1==c:
char1=c
right1=i
elif not char2 or char2==c:
char2=c
right2=i
else:
# print i
if right1<right2:
char1 = c
start=right1+1
right1 = i
else:
char2 = c
start=right2+1
right2 = i
#print 'i={},start={},right1={},righ2={}'.format(i,start,right1,right2)
longest = max(longest, i-start+1)
return longest
sol = Solution()
assert sol.lengthOfLongestSubstringTwoDistinct("cdaba")==3
边工作边刷题:70天一遍leetcode: day 71的更多相关文章
- 边工作边刷题:70天一遍leetcode: day 89
Word Break I/II 现在看都是小case题了,一遍过了.注意这题不是np complete,dp解的time complexity可以是O(n^2) or O(nm) (取决于inner ...
- 边工作边刷题:70天一遍leetcode: day 77
Paint House I/II 要点:这题要区分房子编号i和颜色编号k:目标是某个颜色,所以min的list是上一个房子编号中所有其他颜色+当前颜色的cost https://repl.it/Chw ...
- 边工作边刷题:70天一遍leetcode: day 78
Graph Valid Tree 要点:本身题不难,关键是这题涉及几道关联题目,要清楚之间的差别和关联才能解类似题:isTree就比isCycle多了检查连通性,所以这一系列题从结构上分以下三部分 g ...
- 边工作边刷题:70天一遍leetcode: day 85-3
Zigzag Iterator 要点: 实际不是zigzag而是纵向访问 这题可以扩展到k个list,也可以扩展到只给iterator而不给list.结构上没什么区别,iterator的hasNext ...
- 边工作边刷题:70天一遍leetcode: day 101
dp/recursion的方式和是不是game无关,和game本身的规则有关:flip game不累加值,只需要一个boolean就可以.coin in a line II是从一个方向上选取,所以1d ...
- 边工作边刷题:70天一遍leetcode: day 1
(今日完成:Two Sum, Add Two Numbers, Longest Substring Without Repeating Characters, Median of Two Sorted ...
- 边工作边刷题:70天一遍leetcode: day 70
Design Phone Directory 要点:坑爹的一题,扩展的话类似LRU,但是本题的accept解直接一个set搞定 https://repl.it/Cu0j # Design a Phon ...
- 边工作边刷题:70天一遍leetcode: day 71-3
Two Sum I/II/III 要点:都是简单题,III就要注意如果value-num==num的情况,所以要count,并且count>1 https://repl.it/CrZG 错误点: ...
- 边工作边刷题:70天一遍leetcode: day 71-2
One Edit Distance 要点:有两种解法要考虑:已知长度和未知长度(比如只给个iterator) 已知长度:最好不要用if/else在最外面分情况,而是loop在外,用err记录misma ...
随机推荐
- js限制input输入
1.取消按钮按下时的虚线框,在input里添加属性值 hideFocus 或者 HideFocus=true <input type="submit" value=" ...
- 线段树或树状数组---Flowers
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=4325 Description As is known to all, the blooming tim ...
- 认识Python
web框架:Django.Tornado.Flask Twisted:复杂的异步网络框架 指定解释器 #!/usr/bin/env python #!/usr/bin/python print (&q ...
- 通过NameValuePairsValueProvider对象来获取指定前缀的Key
using System; using System.Collections.Generic; using System.Linq; using System.Web.Http.ValueProvid ...
- GetStartedWithWin10Develop
GetStartedWithWin10Develop 首先要确保已经配置好win10开发环境,开始第一个win10开发的HelloWorld 1.首先创建你的win10项目(示例的项目名称为 Hell ...
- MySQL数据库中字符集的问题
今天在做Hibernate案例,往mysql中写记录的时候,出现ERROR: Incorrect string value: '\xE5\x8A\xA0\xE5\x86\x85...' for col ...
- Oracle SQL Tips
左连接的同时只输出关联表的一条记录 WITH X AS (SELECT 1 ID FROM DUAL UNION SELECT 2 FROM DUAL UNION SELECT 3 FROM DUAL ...
- 操作集合的工具类:Collections
Java提供了一个操作Set.List和Map等集合的工具类:Collections,该工具类提供了大量方法对集合进行排序.查询和修改等操作,还提供了将集合对象置为不可变.对集合对象实现同步控制等方法 ...
- [android] 手机卫士自定义控件的属性
上一节完成的自定义组合控件,灵活性不够,控件的显示信息上,仿照系统属性,自定义自己的属性 上一节组合控件SettingItemView中有三个控件,分别是TextView大标题,TextView描述, ...
- Cocos2d入门--3--向量的应用
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...