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 ...
随机推荐
- HighCharts 饼图
@{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } @section ...
- CSS效果
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs& ...
- ZigZag Conversion1
问题描述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...
- Java [Leetcode 228]Summary Ranges
题目描述: Given a sorted integer array without duplicates, return the summary of its ranges. For example ...
- UVA 10098 Generating Fast, Sorted Permutation
// 给你字符串 按字典序输出所有排列// 要是每个字母都不同 可以直接dfs ^_^// 用前面说的生成排列算法 也可以直接 stl next_permutation #include <io ...
- 【转】mac终端安装node时候,显示“-bash: brew: command not found”,怎么解决?
原文网址:https://segmentfault.com/q/1010000004221389/a-1020000004221408 mac终端安装node时候,显示“-bash: brew: co ...
- printk 驱动调试
驱动的调试,printk()添加调试信息 printk相当于printf的孪生姐妹,它们一个运行在用户态,另一个则在内核态. 需要包含<linux/device.h>或者<linux ...
- HTML5 随音乐节奏变化的频谱图动画
这里将要介绍的HTML5 音频处理接口与Audio标签是不一样的.页面上的Audio标签只是HTML5更语义化的一个表现,而HTML5提供给JavaScript编程用的Audio API则让我们有能力 ...
- 通过userAgent判断手机浏览器类型
我们可以通过userAgent来判断,比如检测某些关键字,例如:AppleWebKit*****Mobile或AppleWebKit,需要注意的是有些浏览器的userAgent中并不包含AppleWe ...
- 使ViewFlipper中的WebView实现手势效果
使ViewFlipper中的WebView实现手势效果 今天写Blog阅读器的时候遇到了这个问题,把它分享给大家,让同样是新手们少走冤枉路始初写这个功能的时候,用过了好多方法,也耗了不少时间去研究 ...