找到最多含有两个不同字符的子串的最长长度。例如: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. MVC把随机产生的字符串转换为图片

    原文:MVC把随机产生的字符串转换为图片 Insus.NET在这篇中<在ASP.NET MVC应用程序中随机获取一个字符串>http://www.cnblogs.com/insus/p/3 ...

  2. TCP/IP 网络编程(六)

    流程模型: 线程模型: 线程的创建和运行流程 #include <pthread.h> int pthread_create(pthread_t * restrict thread, co ...

  3. 注解配置的Spring MVC

    基于注解配置的Spring MVC 简单的HelloWorld实例应用   2.1 问题 使用注解的方式重构helloworld应用案例. 2.2 方案 1. @RequestMapping注解应用 ...

  4. oracle中execute immediate的使用(select/insert/update/delete)(转)

    execute immediate的语法如下: execute immediate 'sql'; execute immediate 'sql_select' into var_1, var_2; e ...

  5. 3D数学学习笔记——笛卡尔坐标系

    本系列文章由birdlove1987编写.转载请注明出处. 文章链接: http://blog.csdn.net/zhurui_idea/article/details/24601215 1.3D数学 ...

  6. shell变一些小技巧

    如果我们定义了一个变量为: file=/dir1/dir2/dir3/my.file.txt 能够用${ }分别替换得到不同的值: ${file#*/}:删掉第一个 / 及其左边的字符串:dir1/d ...

  7. Power Strings (poj 2406 KMP)

    Language: Default Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33205   ...

  8. linux 核心学习书籍

    1<LINUX程序设计> 基础是非常重要的 2<UNIX环境高级编程> 还是基础 ,假设你是搞UNIX/Linux环境下的应用程序编程,那么就看 3, <Linux内核设 ...

  9. 采用Duplicate target database在线恢复秩序oracle datagard图书馆设备

    线上oracle datagard备库由于断电以及误删除从库的归档日志文件,所以导致,备库主库数据不一致,备库须要紧急恢复.以下是大概恢复过程 1,从主库上面备份控制文件[oracle@localho ...

  10. API接口开发 配置、实现、测试

    Yii2 基于RESTful架构的 advanced版API接口开发 配置.实现.测试 环境配置: 开启服务器伪静态 本处以apache为例,查看apache的conf目录下httpd.conf,找到 ...