leetcode[159] Longest Substring with At Most Two Distinct Characters
找到最多含有两个不同字符的子串的最长长度。例如:eoeabc,最长的是eoe为3,其他都为2.
思路:
用p1,p2表示两种字符串的最后一个出现的下标位置。初始p1为0. p2为-1.start初始化为0,表示两种字符串的开头。
只要遍历一次string就可以得到结果了。
首先我们要确定p2的值,那么i要一直到不等于s[p1]的值为止,那么位置就是p2了。
然后继续往后如果来一个字符等于之前两种的其中一种,那么就要更新最后一次出现的下标。根据是谁就更新谁。
如果是新的字符了,那么就要更新start了,start肯定就是小的那个p1+1,因为p1是一种字符的最后一个位置,他的下一位肯定就是另一个字符了。
每次计算大的结束点,p1或者p2,与start中间的个数就是我们要的长度了。最后返回最长的就是了。
因为存在只有一种字符的情况,所以要判断如果p2最后还是-1,那么就返回整个串的长度就是了。代码如下:
因为不能oj,不知道代码有没有bug
int longest159(string s)
{
if (s.size() == ) return ;
int ans = ; int p1 = , p2 = -, start = ; for (int i = ; i < s.size(); i++)
{
if (p2 == -)
{
if (s[i] == s[p1])
continue;
p2 = i;
} if (s[i] == s[p1] || s[i] == s[p2])
s[i] == s[p1] ? p1 = i : p2 = i;
else
{
start = min(p1, p2) + ;
p1 < p2 ? p1 = i : p2 = i;
}
if (max(p1, p2) - start + > ans)
ans = max(p1, p2) - start + ;
}
if (p2 == -)
return s.size();
return ans;
}
leetcode[159] Longest Substring with At Most Two Distinct Characters的更多相关文章
- [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 ...
- ✡ leetcode 159. Longest Substring with At Most Two Distinct Characters 求两个字母组成的最大子串长度 --------- java
Given a string, find the length of the longest substring T that contains at most 2 distinct characte ...
- [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 ...
- [leetcode]340. Longest Substring with At Most K Distinct Characters至多包含K种字符的最长子串
Given a string, find the length of the longest substring T that contains at most k distinct characte ...
- [LeetCode] 340. Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串
Given a string, find the length of the longest substring T that contains at most k distinct characte ...
- 【LeetCode】159. Longest Substring with At Most Two Distinct Characters
Difficulty: Hard More:[目录]LeetCode Java实现 Description Given a string S, find the length of the long ...
- LeetCode 340. Longest Substring with At Most K Distinct Characters
原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/ 题目: Give ...
- 【LeetCode】Longest Substring with At Most Two Distinct Characters (2 solutions)
Longest Substring with At Most Two Distinct Characters Given a string, find the length of the longes ...
- [LC] 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 ...
随机推荐
- extjs_09_定义自己的页面组件
1.项目截图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYWRhbV93enM=/font/5a6L5L2T/fontsize/400/fill/I0J ...
- Eclipse—怎样为Eclipse开发工具中创建的JavaWebproject创建Servlet
在博客<在Eclipse中怎样创建JavaWebproject>中图文并茂的说明了Eclipse中创建JavaWebproject的方法,本篇博客将告诉大家怎样为Eclipse开发工具中创 ...
- 超可爱 纯CSS3实现的小猪、小老鼠、小牛
原文:超可爱 纯CSS3实现的小猪.小老鼠.小牛 利用纯CSS3绘制一些人物.动物.风景已经不是一件新鲜的事情了,主要是利用CSS3可以让直线变成任意的曲线,于是简单的矢量图形绘制对CSS3来说就小菜 ...
- EF中的transaction的使用范例
注意一点: 在EF中使用事物后,对于一个新增的model,在saveChanges后,可以得到该实体的自增ID,但在提交事物之前, 该数据并没有真正的新增到DB中,但此时可以得到model新增的自增I ...
- bigdata_spark_源码修改_本地环境搭建_eclise
Eclipse 下开发调试环境的配置该小节中使用的各项工具分别为:mac (Windows 7)+Eclipse Java EE 4.4.2+Scala 2.10.4+Sbt 0.13.8+Maven ...
- 如何学习ACM
我想对未来的同学有几句话要说: 1 我们几乎没有noi上来的队员,大家只能依靠后期的更加刻苦的努力. 2 我们没有专业的班级或者机制形成职业ACM队伍,所以大家只能尽早的投入进来,用尽一切课余时间去训 ...
- CSharp设计模式读书笔记(20):观察者模式(学习难度:★★★☆☆,使用频率:★★★★★)
观察者模式(Observer Pattern):定义对象之间的一种一对多依赖关系,使得每当一个对象状态发生改变时,其相关依赖对象皆得到通知并被自动更新.观察者模式的别名包括发布-订阅(Publish/ ...
- mysql中国的内容php网页乱码问题
1.更改mysql编码在数据库 character_set_server=utf8 init_connect='SET NAMES utf8' 加入这两行 2.又第一次启动mysql数据库 版权声明: ...
- 通过EA导入数据库存在表结构并生成文档
通过EA导入数据库存在表结构并生成文档 慕课网,程序员升职加薪神器,点击免费学习 目录[-] 导入数据源,表结构 生成表结构的文档 Enterprise Architect 是超级强大项目管理功能 ...
- struts2 模型分配问题和延迟加载问题
傅型号值问题: 首先须要说明的是:Action在请求到达ActionProxy时已经创建出来了,而且对应的创建了一个值栈. 在拦截器到达之前这个图片已经OK了.Action已经创建.并且压入了值栈vs ...