找到最多含有两个不同字符的子串的最长长度。例如: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. Android使用SVG矢量创建很酷的动态效率!

    尊重原创,欢迎转载.转载请注明: FROM  GA_studio   http://blog.csdn.net/tianjian4592 一个真正酷炫的动效往往让人虎躯一震,话不多说.咱们先瞅瞅效果: ...

  2. matlab入门 蜂窝阵列

    B{1,1}=[1,2]; B{1,2}='kitty'; B B = [1x2 double]    'kitty' celldisp(B) B{1} =      1     2 B{2} =   ...

  3. 谈话ZooKeeper(一个)分析ZooKeeper的Quorums机制--预防Split-Brain问题

    使用ZooKeeper学生们应该看到一个参数.它是ZooKeeper超过一半的群集必须节点(Majority)可用的.外来人才在整个集群中可用.在大多数情况下,这种说法是正确的. 谈论这篇文章背后的原 ...

  4. Doug Lea

    如果IT的历史,是以人为主体串接起来的话,那么肯定少不了Doug Lea.这个鼻梁挂着眼镜,留着德王威廉二世的胡子,脸上永远挂着谦逊腼腆笑容,服务于纽约州立大学Oswego分校计算机科学系的老大爷. ...

  5. jQuery.reveal弹出层

    jQuery.reveal弹出层使用 最近用到弹出层,还得自定义UI,原本用的artDialog太庞大,不合适了,于是就找到了这个东西,又小又好用,基础的弹出遮罩都有了,想要什么还不是Coder自己说 ...

  6. 【iOS】多线程GCD

    GCD(Grand Central Dispatch) : 牛逼的中枢调度器.苹果自带,纯C语言实现,提供了许多且强大的函数,它能够提高代码的运行效率与多核的利用率. 一.GCD的基本使用 1.GCD ...

  7. 正则获取URL参数

    一 获取指定URL参数 function getUrlParams(name) { var reg = new RegExp("(^|&)" + name + " ...

  8. JNLP(Java Web Start )(转)

    JNLP(Java Network Launching Protocol )是java提供的一种可以通过浏览器直接执行java应用程序的途径,它使你可以直接通过一个网页上的url连接打开一个java应 ...

  9. Android KK台,联系人列表#集团放置A~Z之前

    更改文件ContactLocaleUtils.java两 (Path:packages/contactsprovider/src/com/android/providers/contacts) 1. ...

  10. decimal system 2016

    Problem Description As we know , we always use the decimal system in our common life, even using the ...