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 ...
随机推荐
- 【制作镜像】virsh
首先进入到图形界面 常用virsh指令: 1)virsh list 列出当前虚拟机列表,不包括未启动的 2)virsh list --all 列出所有虚拟机,包括所有已经定义的虚拟机 3)virsh ...
- Cocos2d-x 3.0 cocostudio骨骼动画的动态换肤
概述 游戏中人物的状态会发生改变,而这种改变通常要通过局部的变化来表现出来.比如获得一件装备后人物形象的改变,或者战斗中武器.防具的损坏等.这些变化的实现就要通过动态换肤来实现.在接下来的这个Demo ...
- MongoDB源码分析——mongod程序源码入口分析
Edit 说明:第一次写笔记,之前都是看别人写的,觉得很简单,开始写了之后才发现真的很难,不知道该怎么分析,这篇文章也参考了很多前辈对MongoDB源码的分析,也有一些自己的理解,后续将会继续分析其他 ...
- java中的JSON对象的使用
申明:没工作之前都没听过JSON,可能是自己太菜了.可能在前台AJAX接触到JSON,这几天要求在纯java的编程中,返回JSON字符串形式. 网上有两种解析JSON对象的jar包:JSON-lib. ...
- 【转】Oracle job procedure 存储过程定时任务
原文:Oracle job procedure 存储过程定时任务 oracle job有定时执行的功能,可以在指定的时间点或每天的某个时间点自行执行任务. 一.查询系统中的job,可以查询视图 --相 ...
- MySQL中EXPLAIN解释命令详解
MySQL中的explain命令显示了mysql如何使用索引来处理select语句以及连接表.explain显示的信息可以帮助选择更好的索引和写出更优化的查询语句. 1.EXPLAIN的使用方法:在s ...
- MVC引用CSS文件の正确姿势
你的css文件目录结构: 将路径写入BundleConfig规则中: using System.Web; using System.Web.Optimization; namespace XXXX { ...
- c# 之 New新知
本人从事.NET工作已经一段时间,毕业之前一直想着做C++的,后来因为各种原因(跟学校导师相关),走向了.NET之路,从而时不时补一下.net的基础知识,因为自己的.NET知识还不是很扎实.近期每天早 ...
- UITextView -- 基础备忘
UITextView 这篇文章只涉及到基本的使用,日后会写一些关于结合TextKit的备忘 基本属性 let screenSize = UIScreen.mainScreen().bounds.siz ...
- 断命windows上卸载node并重装
抠门儿世界500强给前端开发人员用windows windows不支持n模块没法自动升级 不记得何时安装的旧版本node连个uninstaller都找不到 绕道安装nvm path也自动加进去了丫命令 ...