159. Longest Substring with At Most Two Distinct Characters
最后更新
二刷
08-Jan-17
回头看了下一刷的,用的map,应该是int[256]的意思,后面没仔细看cuz whatever I was doing at that time.. wasnt good
做法和LC 76非常像,用2 Pointers + 计数来判断是否满足。
这里“有效读取”的判断标准变成了 count[s.charAt(someIndex)]是否从0递增,和每个循环最后它是否递减回0,以此判断dinstinct是否有变化,其实这个比76的有效读取要稍微好理解一些。
Time: O(N)
Space: Constant space..
public class Solution {
public int lengthOfLongestSubstringTwoDistinct(String s) {
if (s.length() <= 2) return s.length();
int[] count = new int[256];
int temp = 0;
int right = 0;
int maxLength = -1;
for (int left = 0; left < s.length(); left ++) {
while (right < s.length()) {
if (temp == 2 && count[s.charAt(right)] == 0) break;
if (++count[s.charAt(right++)] == 1) {
temp ++;
}
}
if (right - left > maxLength) {
maxLength = right - left;
}
if (--count[s.charAt(left)] == 0) {
temp --;
}
}
return maxLength;
}
}
一刷
18-Dec-2016
怎么和上一题一样的。。。
map快。。
import java.util.Hashtable;
public class Solution
{
public int lengthOfLongestSubstringTwoDistinct(String s)
{
if(s.length() <= 2) return s.length();
Hashtable<Character,Integer> table = new Hashtable<Character,Integer>();
int temp = 0;
int l = 0;
int res = 1;
for(int i = 0; i < s.length();i++)
{
char c = s.charAt(i);
if(!table.containsKey(c)) temp++;
table.put(c,i);
if(temp > 2)
{
temp--;
Iterator iter = table.keySet().iterator();
char iterC = c;
int index = i;
while(iter.hasNext())
{
char tempC = (char)iter.next();
if(table.get(tempC) < index)
{
index = table.get(tempC);
iterC = tempC;
}
}
table.remove(iterC);
l = index + 1;
}
res = Math.max(res,i+1-l);
}
return res;
}
}
159. Longest Substring with At Most Two Distinct Characters的更多相关文章
- [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
Difficulty: Hard More:[目录]LeetCode Java实现 Description Given a string S, find the length of the long ...
- ✡ 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 ...
- [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 ...
- [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 ...
- 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] Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串
Given a string S, find the length of the longest substring T that contains at most two distinct char ...
随机推荐
- Android百度地图
帖子 热搜: 二维码 聊天 二维码扫描 传感器 游戏 定位 手势绘图 小项目 相框 绘图 涂鸦 拨打电话 记事本 定时器 通话记录 短信群发 listview 音乐播放器 项目例子 百度地 ...
- UVa 12661 (单源最短路) Funny Car Racing
题意: 有一个赛车跑道,可以看做一个加权有向图.每个跑道(有向边)还有一个特点就是,会周期性地打开a秒,然后关闭b秒.只有在赛车进入一直到出来,该跑道一直处于打开状态,赛车才能通过. 开始时所有跑道处 ...
- linux下/etc/passwd和/etc/shadow文件
/etc/passwd文件中保存的是用户的账号信息,而/etc/shadow文件中保存的是用户的口令信息. 一 /etc/passwd 一个用户对应着该文件中一行记录,一行记录由若干个字段组成,字段之 ...
- 【应聘】阿里巴巴Java面试题目
原文地址:http://blog.csdn.net/free0sky/article/details/7927275 一.String,StringBuffer, StringBuilder 的区 ...
- JavaScript在IE和Firefox(火狐)的不兼容问题解决方法小结 【转】http://blog.csdn.net/uniqer/article/details/7789104
1.兼容firefox的 outerHTML,FF中没有outerHtml的方法. 代码如下: if (window.HTMLElement) { HTMLElement.prototype.__de ...
- SharePoint 2010遍历文档库中所有的文件,文件夹
转:http://hi.baidu.com/sygwin/item/f99600849d51a12b110ef3eb 创建一个可视WebPart,并拖放一个label控件到ascx文件上,用于显示结果 ...
- [Papers]NSE, $u_3$, Lebesgue space [Cao-Titi, IUMJ, 2008]
$$\bex u_3\in L^p(0,T;L^q(\bbR^3)),\quad \frac{2}{p}+\frac{3}{q}=\frac{2}{3}+\frac{2}{3q},\quad \fra ...
- [Everyday Mathematics]20150206
$$\bex \sen{fg}_{L^1}\leq C\sen{f}_{L^{r,\al}}\sen{g}_{L^{r',\al'}}, \eex$$ 其中 $$\bex f\in L^{r,\al} ...
- String.valueOf()
1. 由 基本数据型态转换成 String String 类别中已经提供了将基本数据型态转换成 String 的 static 方法 也就是 String.valueOf() 这个参数多载的方法 有下 ...
- 第一个简单的android项目
开发平台:windows7+Eclipse+andriod SDK(24.0)+ADT(23.0.4).这个环境的搭建在前一篇文章(Mobile testing下的appium测试)里面已经描述了. ...