✡ 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 ...
随机推荐
- ZooKeeper 编程(一)
Zookeeper的节点都是存放在内存中的,所以读写速度很快.更新日志被记录到了磁盘中,以便用于恢复数据.在更新内在中节点数之前,会先序列化到磁盘中. 为避免单点失效,zookeeper的数据是在多个 ...
- 《BI那点儿事》数据流转换——聚合
聚合转换可以像T-SQL中的函数GROUP BY, Average, Minimum, Maximum, 和 Count一样对数据进行聚合运算.在图中可以看到数据以SampleID分组,对TotalS ...
- 基础笔记6(exception)
1.异常:一种处理错误的机制,将错误和业务分离. throwable的子类 error 和exception exception 分两类:checked (需要捕获处理或者抛出)和unchecked( ...
- 转:VC include 路径解析
VC include 路径解析 要了解vc中使用#include命令包含头文件所搜寻的路径,必须先了解vc中的几种路径: 1. 系统路径 系统路径在vc中是"Tools->Option ...
- Teleport Ultra/Teleport Pro的冗余代码批量清理方法
Teleport Pro 是款优秀的网站离线浏览工具(即网站整站下载工具),Teleport Ultra是其增强版,但使用此系列软件下载的离线网页里会包含大量冗余代码(如tppabs),手动去修改工作 ...
- 【转】关于 Web GIS
以下部分选自2015-03-01出版的<Web GIS从基础到开发实践(基于ArcGIS API for JavaScript)>一书中的前言部分: Web GIS 概念于1994 年首次 ...
- [maven] maven变量
Maven内置变量说明: $${project.basedir}或{basedir} 项目根目录,即包含pom.xml文件的目录 ${project.version}或${version}表示项目版本 ...
- HR函数学习02——分配组织单位
REPORT ZLYHR01. "创建组织单元 成功 DATA:LS_OBJ TYPE OBJEC, LV_STU TYPE GDSTR-SVECT, LV_TIT TYPE CHAR20, ...
- uva 1001(最短路)
题意:在一个三维的奶酪里面有n(n<=100)个洞,老鼠A想到达老鼠B的位置,在洞里面可以瞬间移动,在洞外面的移动速度为10秒一个单位,求最短时间 题解:如果两个洞相交,那么d[i][j]=0: ...
- C#微信公众号开发-高级接口-之模板消息开发,附源码
个人觉得模板消息功能的增加对公众号的作用非常大,可以说是真正意义上的实现了所谓的轻app,商家可以通过模板消息给用户发送重要的信息,交易.预约.消费.邮件.物流等信息.之前我做过的系统通过邮件发送订单 ...