最后更新

二刷

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. fancybox 无效 失效 直接打开页面, ajax 之后 fancybox对更新的数据无效,Jquery失效 无效

    案例:做个聊天室项目,数据都是通过ajax刷新出来的,而对新数据绑定的fancybox均无效,点击直接打开到了新页面而不是弹窗,解决方法其实很简单   简单分析:ajax加载内容是在$(documen ...

  2. js array 数组删除元素

    /* * 方法:Array.remove(dx) * 功能:根据元素位置值删除数组元素. * 参数:元素值 * 返回:在原数组上修改数组 */ Array.prototype.baoremove = ...

  3. 完整cocos2d-x编译Andriod应用过程

    作者:何卫 转载请注明,原文链接:http://www.cnblogs.com/hewei2012/p/3366969.html 其他平台移植:http://cocos2d.cocoachina.co ...

  4. 【iOS-cocos2d-X 游戏开发之九】Cocos2dx利用CCSAXParser解析xml数据&CCMutableDictionary使用与注意!

    本站文章均为李华明Himi原创,转载务必在明显处注明:转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/iphone-cocos2dx/694.html ☞ ...

  5. ti processor sdk linux am335x evm /bin/setup-minicom.sh hacking

    #!/bin/sh # # ti processor sdk linux am335x evm /bin/setup-minicom.sh hacking # 说明: # 本文主要对TI的sdk中的s ...

  6. RPi 2B Documentation

    /********************************************************************** * RPi 2B Documentation * 声明: ...

  7. datatables 参数详解(转)

    //@translator codepiano //@blog codepiano //@email codepiano.li@gmail.com //尝试着翻译了一下,难免有错误的地方,欢迎发邮件告 ...

  8. linux - 开机启动thunderbird、chromium

    cd ~/.config/autostart/(没有autostart,自己 mkdir autostart) vim thunderbird.desktop,输入以下: [Desktop Entry ...

  9. hdu 5335 Walk Out(bfs+斜行递推) 2015 Multi-University Training Contest 4

    题意—— 一个n*m的地图,从左上角走到右下角. 这个地图是一个01串,要求我们行走的路径形成的01串最小. 注意,串中最左端的0全部可以忽略,除非是一个0串,此时输出0. 例: 3 3 001 11 ...

  10. [转] C#实现自动化Log日志

    qing2005原文地址 C#实现自动化Log日志 在开发项目的时候,我们不免要使用Log记录日志,使用最多的是Log4Net和EntLib Log,在需要记录日志的代码处加入log.Write(日志 ...