✡ 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 ...
随机推荐
- loadrunner常用术语
1.场景 在loadrunner中主要表现为controller中设计与执行测试用例中的用户场景.主要工作有,在controller中选择虚拟用户脚本.设置虚拟用户数量.配置虚拟用户运行时的行为.选择 ...
- 阿里云对象存储OSS————跨域资源共享(CORS)(m3u8 无法加载m3u8:跨域访问被拒绝)
今天在做视频直播录像的时候,添加一个录制APP的.M3U8文件到OSS的一个test文件中存储,结果是访问不到了: 提示:无法加载m3u8:跨域访问被拒绝!!!!! 项目代码测试地址:https:// ...
- mysql的初识--DOS下的简单命令
DOS下进入 1.通过程序中的mySQL的:MySQL 5.6 Command Line Client直接进入mySQL的命令行: 2.或者通过WIn+R-->输入cmd,然后C:等一层一层找到 ...
- 程序设计入门——C语言 第1周编程练习 1逆序的三位数(5分)
第1周编程练习 查看帮助 返回 第1周编程练习题,直到课程结束之前随时可以来做.在自己的IDE或编辑器中完成作业后,将源代码的全部内容拷贝.粘贴到题目的代码区,就可以提交,然后可以查看在线编译和运 ...
- ionic build android--> Build failed with an exception. Execution failed for task ':processDebugResources'.
执行 ionic build android, ionic 自动化生成安卓apk包, 出现以上报错的原因为:打包的文件中含有中文名,把中文名的文件去掉或改名即可打包成功.
- python中lambda表达式应用
对于简单的函数,也存在一种简便的表示方式,即:lambda表达式 #普通函数1 def func(a): return a+1 print 'test1_func0:',func(1000)4#lam ...
- 6/13 Sprint2 看板和燃尽图
部分页面展示
- python练习题代码
1.打印出相应规则的字母 zm='ABCDEFGHIJKLMNOPQRSTUVWXYZ' >>> for i in range(0,len(zm)): if i==0: print ...
- Python中 filter | map | reduce | lambda的用法
1.filter(function, sequence):对sequence中的item依次执行function(item),将执行结果为True的item组成一个List/String/Tupl ...
- 源代码tfs to git
TFSàgit可以保留完整历史记录,方法: https://github.com/git-tfs/git-tfs 系统变量的path里加上: ;C:\Program Files (x86)\Git\b ...