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 ...
随机推荐
- 在HTML中插入回车换行
在制作EPUB的时候,发现原来收集的html文本根本就没有换行,这个在代码里面是无法忍受的,因为看着比较混乱,全部都是在一行里面,这个就像写作文的时候,你看到的文字全部都是在一行里面显示出来的,根本就 ...
- MateSublg
MateSublg 说明 使用MetaWeblog的方式提交文章,并自动上传图片. 本插件的官方地址:MateSublg – Sollyu博客 本插件的开源地址:sollyu / MetaSubolg ...
- ASP.NET中的特殊路径标识"~"
/ 表示网站根目录(从域名开始), ../ 表示上级目录, ./表示当前目录- 是ASP.NET定义的特殊符号,是ASP.NET内部进行定义推荐的用法,-代表从应用根目录开始定义应用根目录与网站根目录 ...
- Python中 if __name__ == '__main__': 详解
一个python文件就可以看作是一个python的模块,这个python模块(.py文件)有两种使用方式:直接运行和作为模块被其他模块调用. __name__:每一个模块都有一个内置属性__name_ ...
- grunt-mac上安装运行构建工具的总结(一)
安装node.js brew install node.js 安装grunt npm install -g grunt-cli 1.新建package.json,配置 { "name&quo ...
- svn命令操作
非超级用户:sudo 命令 svn info 查看svn地址 svn co 地址 检出仓库
- Windows BAT字符串操作
::操作系统:windows xp sp3 @echo off ::初始化变量set str1=This is string1set str2=This is string2set str3=This ...
- STM32库函数开发使用总结
一.外设常具备的几类寄存器 控制寄存器xxx_CR (Control/Configuration Register): 用来配置.控制响应外设的工作方式,如GPIOx_CRL.AFIO_EXTICR1 ...
- 解决vsftpd 2.2.2读取目录列表失败的问题
该错误是由iptables的配置引起的,临时的解决方法是执行如下命令: [root@localhost soft]# modprobe ip_nat_ftp 再次登陆列表正常啦! 但当你重新启动服务器 ...
- AndroidStudio Gradle版本不匹配问题
报错信息: p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } Error:(1, 1) A problem occurr ...