72.Minimum Window Substring(最小子串窗口)
Level:
Hard
题目描述:
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
Example:
Input: S = "ADOBECODEBANC", T = "ABC"
Output: "BANC"
Note:
- If there is no such window in S that covers all characters in T, return the empty string
""
. - If there is such window, you are guaranteed that there will always be only one unique minimum window in S.
思路分析:
知识点:滑动窗口
关键点一:像这种找子串的可以使用滑动窗口Sliding Window来做。这个窗口由left和right来约束[left,right]这个闭区间,刚开始知识left=right=0,然后left不变,right不断增长,直到到达某个值,left和right一起增长,这样就实现了窗口的创建和向下滑动。
关键点二:对于p中出现的字符以及次数需要记下来。可以使用一个数组ascaiiNums,长度为256,是因为ascaii一共只有256个,初始化的值表示角标对应的ascaii字符在p中出现的次数,那么没有出现过的就是0.之后如果s中出现过该字符,那么数组对应位置的值就减一。如果减之后的值大于等于0,就说明p中该字符出现了一次,这时p中未出现的字符数量count就减一。之所以要判断是>=0,是因为如果数组中原来的值是0(说明p中没有出现),那么减一后就成了负的,这种情况下p中剩下未出现的字符的count数值保持不变。如果count的值为0,说明p中的字符全出现了,找到一个符合条件的子串,left就是子串的首地址。之后要向右滑动窗口,移动的方法是left++,right++,更新p中未出现的字符个数count。
代码:
public class Solution{
public String minWindow(String s,String t){
if(s==null||t==null||s.length()==0||t.length()==0)
return null;
String res="";
int []map=new int [256];
int match=t.length();
int left=0; //窗口左端
for(int i=0;i<t.length();i++){
map[t.charAt(i)]++;
}
int minlen=Integer.MAX_VALUE;
for(int i=0;i<s.length();i++){//i充当窗口的右端
map[s.charAt(i)]--;
if(map[s.charAt(i)]>=0)
match--; //证明该字符在t中
if(match==0){
while(map[s.charAt(left)]<0){
map[s.charAt(left)]++;
left++; //这些字符都不存在与t中
}
if((i-left+1)<minlen){//更新最短子串
minlen=i-left+1;
res=s.substring(left,i+1);
}
match++; //寻找下一个包含t的串
map[s.charAt(left)]++;
left++;
}
}
return minlen==Integer.MAX_VALUE?"":res;
}
}
72.Minimum Window Substring(最小子串窗口)的更多相关文章
- lintcode 中等题:minimum window substring 最小子串覆盖
题目 最小子串覆盖 给定一个字符串source和一个目标字符串target,在字符串source中找到包括所有目标字符串字母的子串. 样例 给出source = "ADOBECODEBANC ...
- [Leetcode] minimum window substring 最小字符窗口
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- [leetcode]76. Minimum Window Substring最小字符串窗口
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- [LeetCode] Minimum Window Substring 最小窗口子串
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- Minimum Window Substring, 包含子串的最小窗口,双指针
问题描述:给定字符串S,子串T,求S中包含T的最小窗口 Given a string S and a string T, find the minimum window in S which will ...
- [LeetCode] 76. Minimum Window Substring 最小窗口子串
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- 【LeetCode】76. Minimum Window Substring 最小覆盖子串(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 日期 题目地址: https://leet ...
- [LeetCode] 727. Minimum Window Subsequence 最小窗口子序列
Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequenceof ...
- Minimum Window Substring @LeetCode
不好做的一道题,发现String Algorithm可以出很多很难的题,特别是多指针,DP,数学推导的题.参考了许多资料: http://leetcode.com/2010/11/finding-mi ...
随机推荐
- alert(1) to win 16
- MySQL数据库5事务、视图、触发器、函数、数据库的备份
目录 一.事务(important) 1.1什么是事务? 1.2解决办法 1.2.1事务的语法 1.2.2使用事务解决转账问题代码演示 1.2.3rollback 1.3事务的特性(important ...
- hdu 1693 : Eat the Trees 【插头dp 入门】
题目链接 题意: 给出一个n*m大小的01矩阵,在其中画线连成封闭图形,其中对每一个值为1的方格,线要恰好穿入穿出共两次,对每一个值为0的方格,所画线不能经过. 参考资料: <基于连通性状态压缩 ...
- 【Java】java.sql.SQLDataException: Cannot determine value type from string
报错如下: There was an unexpected error (type=Internal Server Error, status=500). Error attempting to ge ...
- 20180826(05)- Java URL处理
Java URL处理 URL(Uniform Resource Locator)中文名为统一资源定位符,有时也被俗称为网页地址.表示为互联网上的资源,如网页或者FTP地址. 本章节我们将介绍Java是 ...
- Tomcat的配置文件server.xml叙述
元素名属性解释 server port 指定一个端口,这个端口负责监听关闭tomcat的请求shutdown 指定向端口发送的命令字符串 servicename 指定service ...
- HDU4089 Activation(概率DP+处理环迭代式子)
题意:有n个人排队等着在官网上激活游戏.Tomato排在第m个. 对于队列中的第一个人.有一下情况: 1.激活失败,留在队列中等待下一次激活(概率为p1) 2.失去连接,出队列,然后排在队列的最后(概 ...
- BLOB类型的字段用于存储二进制数据
MySQL中,BLOB是个类型系列,包括:TinyBlob.Blob.MediumBlob.LongBlob,这几个类型之间的唯一区别是在存储文件的最大大小上不同. MySQL的四种BLOB类型类型 ...
- 【SpringBoot】 Java中如何封装Http请求,以及JSON多层嵌套解析
前言 本文中的内容其实严格来说不算springboot里面的特性,属于JAVA基础,只是我在项目中遇到了,特归纳总结一下. HTTP请求封装 目前JAVA对于HTTP封装主要有三种方式: 1. JAV ...
- Linux 路由表详解及 route 命令详解
参考资料 Linux 内核的路由表 通过 route 命令查看 Linux 内核的路由表: [root@VM_139_74_centos ~]# route Kernel IP routing tab ...