Minimum Window Substring &&& Longest Substring Without Repeating Characters 快慢指针,都不会退,用hashmap或者其他结构保证
1
public class Solution {
public static int lengthOfLongestSubstring(String s) {
char[] arr = s.toCharArray();
int pre = 0;
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
for (int i = 0; i < arr.length; i++) {
if (!map.containsKey(arr[i])) {
map.put(arr[i], i);
} else {
pre = pre > map.size() ? pre : map.size();
i = map.get(arr[i]);
map.clear();
}
}
return Math.max(pre, map.size());
}
}
public class Solution {
public String minWindow(String S, String T) {
char s[]=S.toCharArray();
if(S=="")return "";
int beg=0;
int end=0;
int d[]=new int[128];
int size=0;
for(int i=0;i<T.length();i++)
{
if(d[t[i]]==0) size++;
d[t[i]]++;
}
int d2[]=new int[128];
int s1=0;
boolean flag=false;
while(end<s.length)
{
if(d[s[end]]==0) {end++;continue;}
d2[s[end]]++;
if(d2[s[end]]==d[s[end]]) s1++;
end++;
if(s1==size)
{
while(d2[s[beg]]>d[s[beg]]||d[s[beg]]==0) {d2[s[beg]]--;beg++;}
flag=true;
break;
}
}
if(!flag) return "";
int aend=end-1;
int abeg=beg;
int amin=end-beg;
while(end<s.length)
{
if(d[s[end]]==0){end++;continue;}
d2[s[end]]++;
while((d2[s[beg]]>d[s[beg]])||d[s[beg]]==0) {
d2[s[beg]]--;beg++;
if(end-beg+1<amin)
{amin=end-beg+1;
aend=end;
abeg=beg;
}
}
end++;
}
//
return S.substring(abeg,aend+1);
}
}
Minimum Window Substring &&& Longest Substring Without Repeating Characters 快慢指针,都不会退,用hashmap或者其他结构保证的更多相关文章
- [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 ...
- [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] Minimum Window Subsequence 最小窗口序列
Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequence of ...
- [LeetCode] 727. Minimum Window Subsequence 最小窗口序列
Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequence of ...
- [LeetCode] 3. Longest Substring Without Repeating Characters 解题思路
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- [LeetCode] Longest Substring Without Repeating Characters (LinkedHashSet的妙用)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [Swift]LeetCode3. 无重复字符的最长子串 | Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
随机推荐
- HDU 4706 Children's Day(简单模拟)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4706 题目大意:要用‘a’-‘z’ 26个小写字母输出倒着写得字母'N'的形状,例如 a ebdfc ...
- HDU 4294 A Famous Equation(DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4249 题目大意:给一个a+b=c的表达式,但是a.b.c中部分位的数字丢失,并用?代替,问有多少种方案 ...
- (转)UIButton用法详解一
(注明 来源网址 http://blog.csdn.net/cheneystudy/article/details/8115092)这段代码动态的创建了一个UIButton,并且把相关常用的属性都列举 ...
- C# 利用TextBox的Text属性实现换行加字符 "\r\n"
要让一个TextBox显示多行文本就得把它的Multiline属性设置为true,可是如果你是要把TextBox的Text属性设置多行文本时,换行符由两个字符组成:"\r\n". ...
- ajax 的基本原理
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- C#面向对象的学习笔记
1.面向对象的3要素: 封装:将不需要显示的代码封装到一个方法中,只对外提供方法名,用户不需关心内部实现. 继承:子类继承父类,公用父类的代码,大大提高了代码的重用,贴近生活也符合人类的编程思想. 多 ...
- jQuery设置checkbox全选(区别jQuery版本)
jQuery设置checkbox全选在网上有各种文章介绍,但是为什么在我们用他们的代码的时候就没有效果呢? 如果你的代码一点错误都没有,先不要急着怀疑人家代码的正确性,也许只是人家跟你用的jQuery ...
- svn命令操作
非超级用户:sudo 命令 svn info 查看svn地址 svn co 地址 检出仓库
- PHP全局变量
1.global 关键字 2.$GLOBALS 3.使用静态变量
- SQL SERVER 2008 使用TDE加密和解密
SQL SERVER 2008 加密和解密,这样的文件在互联网上不胜枚举,本文的寓意还是一样,一为记录,二可以为开发者提供在实现过程中的注意事项. TDE: Transparent data encr ...