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 ...
随机推荐
- Cogs 1008. 贪婪大陆(树状数组)
贪婪大陆 难度等级 ★★ 时间限制 1000 ms (1 s) 内存限制 128 MB 测试数据 10 简单对比 输入文件:greedisland.in 输出文件:greedisland.out 简单 ...
- 暑假集训(1)第八弹 -----简单迷宫(Poj3984)
Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, ...
- (转)UIButton用法详解一
(注明 来源网址 http://blog.csdn.net/cheneystudy/article/details/8115092)这段代码动态的创建了一个UIButton,并且把相关常用的属性都列举 ...
- Headfirst设计模式的C++实现——抽象工厂(Abstract Factory)
Dough.h #ifndef _DOUGH_H #define _DOUGH_H class Dough { }; #endif ThinCrustDough.h #ifndef _THIN_CRU ...
- nagios 完全配置手册
Linux下Nagios的安装与配置 一.Nagios简介 Nagios是一款开源的电脑系统和网络监视工具,能有效监控Windows.Linux和Unix的主机状态,交换机路由器等网络设置,打印机 ...
- 深入理解Python中的生成器
生成器(generator)概念 生成器不会把结果保存在一个系列中,而是保存生成器的状态,在每次进行迭代时返回一个值,直到遇到StopIteration异常结束. 生成器语法 生成器表达式: 通列表解 ...
- 『奇葩问题集锦』Ruby 切换淘宝源报错WARNING: Error fetching data: SSL_connect returned=1 errno=0 state=SSLv3 read s erver certificate B: certificate verify failed
===>首先需要使用https<===https://ruby.taobao.org/ 第一步 下载http://pan.baidu.com/s/1kU0rxtH 复制到ruby安装的根目 ...
- 收藏一个匹配html内容的文章
http://blog.csdn.net/donglynn/article/details/35788879
- Java RMI(远程方法调用)开发
参考 https://docs.oracle.com/javase/7/docs/platform/rmi/spec/rmi-arch2.html http://www.cnblogs.com/wxi ...
- python学习笔记enumerate()与range(len)运用及赋值小计
#!/uer/bin/env python # _*_ coding: utf-8 _*_ #格式1 a = 'abc' for i in range(len(a)): print a[i],'(%d ...