✡ 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 ...
随机推荐
- 《BI那点儿事》数据流转换——派生列
派生列转换通过对转换输入列应用表达式来创建新列值. 表达式可以包含来自转换输入的变量.函数.运算符和列的任意组合. 结果可作为新列添加,也可作为替换值插入到现有列. 派生列转换可定义多个派生列,任何变 ...
- 轻松解决Linux安装Eclipse方法
随着Linux的发展,很多人开始学习Linux系统,你了解Linux系统么?你是Linux系统的应用者么?本文为你详细介绍Linux安装Eclipse,为你在学习Linux安装Eclipse时起一定的 ...
- Scrum Meeting 1-20151201
任务安排 姓名 今日任务 明日任务 困难 董元财 学习下拉刷新的实现 完成下拉刷新的实现 手机的点击动作长按和下拉有类似的地方,比较难解决 胡亚坤 学习圆形头像代码设计 完成圆形头像代码设计 无 刘猛 ...
- 如何搭建一个linux服务器
1, 首先 下载一个linux server 系统镜像 ubuntu 64bit下载 http://www.ubuntu.com/download/server/thank-you/?version= ...
- TCP && UDP
TCP(Transmission Control Protocol,传输控制协议) 是面向连接的协议,也就是说,在收发数据前,必须和对方建立可靠的连接. TCP建立连接要进行3次握手,而断开连接要进行 ...
- hdu 1032
题目的意思是把输入的i,j 从i到j的每一个数 做循环,输出循环次数最大的值 易错的地方:做循环是容易直接用i进行计算 i=i/2:或i=i*3+1: 这样i的值改变就不能在做下面数的循环 #incl ...
- ocp 1Z0-051 141题---感觉有问题
141. View the Exhibit and examine the structure of CUSTOMERS and GRADES tables. You need to display ...
- Windows下搭建IOS开发环境(一)
原文:http://blog.csdn.net/shangyuan21/article/details/18153605 我们都知道开发iPhone等ios平台的移动应用时需要使用Mac本,但是Mac ...
- bzoj 2878: [Noi2012]迷失游乐园
#include<iostream> #include<cstring> #include<cstdio> #define M 100005 #define ld ...
- SharePoint 2013 开发——其他社交功能
博客地址:http://blog.csdn.net/FoxDave 上一篇讲了如何获取用户配置文件的相关属性,它属于SharePoint 2013社交功能的一个小的构成部分.社交功能是SharePoi ...