找到最多含有两个不同字符的子串的最长长度。例如: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的更多相关文章

  1. [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 ...

  2. ✡ 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 ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. 【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 ...

  7. LeetCode 340. Longest Substring with At Most K Distinct Characters

    原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/ 题目: Give ...

  8. 【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 ...

  9. [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 ...

随机推荐

  1. WPF六个控制概述

    在线演示:http://v.youku.com/v_show/id_XNzA0NjU1Mjk2.html 清晰版视频+代码下载:http://115.com/lb/5lbcftnrfo9s 一.简单介 ...

  2. 金蝶K3管理软件PDA条码解决方式,盘点机与金蝶K3无缝对接

    申明:以上文字为"武汉汉码科技有限公司"原创,转载时务必注明出处. 技术分享,沟通你我,共同进步!www.hanma-scan.com 原帖:http://www.hanma-sc ...

  3. WebService之CXF注解报错(一)

    WebService之CXF注解 1.详细报错例如以下 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] ...

  4. JavaScript权威指南科13章 webj浏览器avascript

    13.1 clientjavascript window对象是所有clientjavascript特点和api主要的接入点.它代表了一个浏览器窗口,通过window对象引用它. window 方法 a ...

  5. 学习笔记 broswerify + watchify + beefy

    broswerify “Browserify lets you require('modules') in the browser by bundling up all of your depende ...

  6. osx launchpad删除图标

    安装了个parallels desktop之后,OSX中的launchpad中的图标多了不少,但是好多都不是我自己想要的,我们该怎么删除或者改动呢,以下介绍一些方法: ①直接操作Appications ...

  7. Hybrid----U采用IWebView演出PDF和其他文件

    App如果你需要显示pdf.word档,在这个时候有没有其他控制,比UIWebView更适合,这是高度抽象的技术细节,可以非常easy采用 UIWebView打开文件类型列表 watermark/2/ ...

  8. Javascript学习2 - Javascript中的表达式和运算符

    原文:Javascript学习2 - Javascript中的表达式和运算符 Javascript中的运算符与C/C++中的运算符相似,但有几处不同的地方,相对于C/C++,也增加了几个不同的运算符, ...

  9. 大数据系列修炼-Scala课程02

    Scala数组操作实战详解 接着昨天的课程,下面我们继续学习关于Scala数组操作详解.Scala数组的定义 //数组定义 //定长数组格式 /** * val arrayName = new Arr ...

  10. hdu Hat's Tea

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1288 去买茶,需要正好的钱才行,另外花的钱的个数最多  其实是一个简单的贪心问题,小的多取一点,多的少 ...