✡ 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 characters.
For example, Given s = “eceba”,
T is "ece" which its length is 3.
给一个字符串,求这个字符串中,由两个字母组成的,连续的最长子串的长度。
虽然是hard,但是感觉并没有什么难度。
用ch1和preCh记录当前两个字母,preCh记录的是上一个字母(s.charAt(i-1)),same记录的是preCh这个字母重复出现的次数,这样出现第三个字母的时候,就可以直接得出从0到i由后两个字母组成的长度为same+1,并没有使用其他的数据结构。
时间O(n),空间O(1).
public class Solution {
public int lengthOfLongestSubstringTwoDistinct(String s) {
int len = s.length();
if (len < 3){
return len;
}
char ch1 = s.charAt(0);
int same = 0;
while (same < len && s.charAt(same) == ch1){
same++;
}
if (same == len){
return len;
}
char preCh = s.charAt(same);
int result = same + 1;
int ans = same + 1;
int i = same + 1;
same = 1;
for (; i < len; i++){
if (s.charAt(i) == preCh){
result++;
same++;
} else if (s.charAt(i) == ch1){
result++;
same = 1;
ch1 = preCh;
preCh = s.charAt(i);
} else {
ch1 = preCh;
preCh = s.charAt(i);
ans = Math.max(ans, result);
result = same + 1;
same = 1;
}
}
ans = Math.max(ans, result);
return ans;
}
}
2、还可以利用hashMap来做:(参考discuss)
Map数目小于3的时候将字母和他的位置加入Map中。
如果是大于等于3,那么找出距离位置hi最远的一个字母(leftMost),删掉,从leftMost的下一个字母开始到当前位置hi就是当前两个字母的长度。
public class Solution {
public int lengthOfLongestSubstringTwoDistinct(String s) {
if(s.length() < 1) return 0;
HashMap<Character,Integer> index = new HashMap<Character,Integer>();
int lo = 0;
int hi = 0;
int maxLength = 0;
while(hi < s.length()) {
if(index.size() <= 2) {
char c = s.charAt(hi);
index.put(c, hi);
hi++;
}
if(index.size() > 2) {
int leftMost = s.length();
for(int i : index.values()) {
leftMost = Math.min(leftMost,i);
}
char c = s.charAt(leftMost);
index.remove(c);
lo = leftMost+1;
}
maxLength = Math.max(maxLength, hi-lo);
}
return maxLength;
}
}
3、其实不算算法,就是把s先转换成char[]。这样就会达到最快。
public class Solution {
public int lengthOfLongestSubstringTwoDistinct(String s) {
int len = s.length();
if (len < 3){
return len;
}
char[] words = s.toCharArray();
char ch1 = words[0];
int i = 1;
while (i < len && words[i] == ch1){
i++;
}
if (i == len){
return len;
}
int same = 1;
char preCh = words[i];
int ans = i + 1;
int result = ans;
i++;
while (i < len){
if (words[i] == preCh){
result++;
same++;
} else if (words[i] == ch1){
result++;
same = 1;
ch1 = preCh;
preCh = words[i];
} else {
ch1 = preCh;
preCh = words[i];
ans = Math.max(ans, result);
result = same + 1;
same = 1;
}
i++;
}
ans = Math.max(ans, result);
return ans;
}
}
✡ leetcode 159. Longest Substring with At Most Two Distinct Characters 求两个字母组成的最大子串长度 --------- java的更多相关文章
- [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至多包含两种字符的最长子串
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
找到最多含有两个不同字符的子串的最长长度.例如:eoeabc,最长的是eoe为3,其他都为2. 思路: 用p1,p2表示两种字符串的最后一个出现的下标位置.初始p1为0. p2为-1.start初始化 ...
- [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 ...
随机推荐
- 扩展XAF模型信息实现自定义功能
如何隐藏 web listview 的 编辑控制列如下图: 这列怎么让它隐藏? 感谢[少侠]XAF_杨东 提供解答!感谢XAF_小学生整理. A: 注册自定义接口IModelListViewExt ...
- 深入浅出设计模式——建造者模式(Builder Pattern)
模式动机无论是在现实世界中还是在软件系统中,都存在一些复杂的对象,它们拥有多个组成部分,如汽车,它包括车轮.方向盘.发送机等各种部件.而对于大多数用户而言,无须知道这些部件的装配细节,也几乎不会使用单 ...
- Spring 数据库配置用户名和密码加密
单个数据库配置 : 一般spring容器启动时,通过PropertyPlaceholderConfigurer类读取jdbc.properties文件里的数据库配置信息.通过这个原理,我们把加密后的数 ...
- python的class的__str__()和__repr__()函数
repr(object) 返回一个可以用来表示对象的可打印字符串首先,尝试生成这样一个字符串,将其传给 eval()可重新生成同样的对象 否则,生成用尖括号包住的字符串,包含类型名和额外的信息(比如地 ...
- 转:[置顶] 从头到尾彻底理解KMP(2014年8月22日版)
[置顶] 从头到尾彻底理解KMP(2014年8月22日版)
- [WebLoad] 使用WebLoad进行Web Application 性能测试的流程
1. 打开WebLOAD IDE录制或编写一个脚本文件,成功后会生成一个后缀为“.wlp”的文件. 2. 打开WebLOAD Console创建一个Load Template,创建过程当中需要添加“. ...
- ionic build android--> Build failed with an exception. Execution failed for task ':processDebugResources'.
执行 ionic build android, ionic 自动化生成安卓apk包, 出现以上报错的原因为:打包的文件中含有中文名,把中文名的文件去掉或改名即可打包成功.
- mysql 中关于周和月份的表示
本周:YEARWEEK(date_format(create_time,'%Y-%m-%d')) = YEARWEEK(now()) 上周:YEARWEEK(date_format(create_ti ...
- 向量时钟算法简介——本质类似MVCC
转自:http://blog.chinaunix.net/uid-27105712-id-5612512.html 一.使用背景 先说一下需要用到向量时钟的场景.我们在写数据时候,经常希望数据不要存储 ...
- JavaWeb chapter 4 Servlet处理HTTP请求
1. GET/POST提交方法: 用户在网页上点击一个超链接:(get) 用户提交在网页上提交表单:(post或者get) 用户在浏览器地址栏输入URL地址并回车(get) 2. 默认情况下都是使 ...