最后更新

二刷

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的更多相关文章

  1. [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 ...

  2. 【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 ...

  3. ✡ 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 ...

  4. [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 ...

  5. [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 ...

  6. leetcode[159] Longest Substring with At Most Two Distinct Characters

    找到最多含有两个不同字符的子串的最长长度.例如:eoeabc,最长的是eoe为3,其他都为2. 思路: 用p1,p2表示两种字符串的最后一个出现的下标位置.初始p1为0. p2为-1.start初始化 ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. iOS开发:Swift多线程NSOperation的使用

    介绍: NSOperation是基于GCD实现,封装了一些更为简单实用的功能,因为GCD的线程生命周期是自动管理,所以NSOperation也是自动管理.NSOperation配合NSOperatio ...

  2. [转] jQuery Infinite Ajax Scroll(ias) 分页插件介绍

    原文链接:http://justflyhigh.com/index.php/articlec/index/index.php?s=content&m=aticle&id=91 Infi ...

  3. css清除浮动的两种方式(clearfix和clear)

    最近总是在用浮动,这两种方式总是浮现在眼前,或者说去掉父级和同级浮动样式总在思考中.两种方式怎么写都在base.css中. 在做瑞祥之旅的过程中,还是吃了一个大亏,就是清除浮动,不管是同级还是父级,都 ...

  4. CoreData 基本操作方法封装

    转:http://blog.csdn.net/marujunyy/article/details/18500523 为了方便使用CoreData 封装了几个扩展类,使用方法和类文件如下: //首先需要 ...

  5. poj 4052(ac自动机)

    题意:自己百度吧!! 分析:就是通过它的fail指针来找出它的子串就行了,这题其实不难的.这好像还是金华邀请赛的题哦! 代码实现: #include<cstdio> #include< ...

  6. Unable to execute dex: method ID not in [0, 0xffff]: 65536

    http://ingramchen.io/blog/2014/09/prevention-of-android-dex-64k-method-size-limit.html

  7. VS2010生成Qt程序图标修改方法

    转自:http://blog.csdn.net/simeone18/article/details/7344547 1.准备ico文件,nuistcard.ico 2.在nuistcard工程目录,建 ...

  8. Clang Language Extensions

    Xcode 本文是自<Clang Language Extensions> 中选取部分与 Objective-C 相关的内容翻译,由于作者水平有限,如存在理解错误或翻译不到位的地方,还请指 ...

  9. web前端开发分享-css,js入门篇(转)

    转自:http://www.cnblogs.com/jikey/p/3600308.html 关注前端这么多年,没有大的成就,就入门期间积累了不少技巧与心得,跟大家分享一下,不一定都适合每个人,毕竟人 ...

  10. Java-note-调试小技巧

    使用 Eclipse 调试 Java 程序的 10 个技巧 英文原文:Again! – 10 Tips on Java Debugging with Eclipse 你应该看过一些如<关于调试的 ...