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 ...
随机推荐
- WebService之CXF注解报错(一)
WebService之CXF注解 1.详细报错例如以下 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] ...
- 软测试综述——PV操作
在操作系统中,进程之间常常会存在相互排斥(都须要共享独占性资源时)和同步(完毕异步的两个进程的协作)两种关系.而信号量和PV操作完美有效的处理了这两种情况. 相互排斥:就好比过独木桥,一 ...
- "伪中国移动client"--伪基站诈骗
一.简单介绍: 近日,百度安全实验室发现一款"伪中国移动client"病毒.犯罪分子通过伪基站方式大量发送伪10086的短信,诱导用户点击钓鱼链接:并在钓鱼页面诱导用户输入网银账号 ...
- HDInsight HBase概观
HDInsight HBase概观 什么是HBase的? HBase它是基于HadoopApache开源NoSQL数据库.它提供了很多非结构化和半结构化数据一致性的随机存取能力的.它是仿照谷歌的Big ...
- POJ1080 Human Gene Functions 动态规划 LCS的变形
题意读了半年,唉,给你两串字符,然后长度不同,你能够用'-'把它们补成同样长度,补在哪里取决于得分,它会给你一个得分表,问你最大得分 跟LCS非常像的DP数组 dp[i][j]表示第一个字符串取第i个 ...
- crmsql句子的实体关系实体字段的信息窗口
在crm里面怎样用sql语句查询这些信息? 查询实体信息: --查询实体信息,实体名称:account select * from MetadataSchema.Entity where name= ...
- NYNU_省赛选拔题(10)
题目描述 Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recog ...
- linux_inux中find批量删除空文件及空文件夹脚本
1.{}和之间有一个空格 2.find . -name 之间也有空格 3.exec 是一个后续的命令,{}内的内容代表前面查找出来的文件 linux下批量删除空文件(大小等于0的文件)的方法 rm - ...
- Web文件(图片)上传方法
在开放Web应用程序的时候经常会遇到图片或者是文件上传的模块,这里就是该模块的实现的后台方法 上传图片方法 /// <summary> /// 功能:上传图片方法 /// </sum ...
- java编程接口(5) ------ button和button组
这篇文章是由自己的学习笔记,欢迎转载,但请注明出处:http://blog.csdn.net/jesson20121020 了解了布局管理器和Swing事件模型,那么剩下的就是Swing 的各个组件了 ...