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;}
}
转载于:https://www.cnblogs.com/yjxyy/p/11098228.html
72.Minimum Window Substring(最小子串窗口)相关推荐
- Minimum Window Substring 最小覆盖子串算法
转载:https://blog.csdn.net/fly_yr/article/details/51134340 题目 最小子串覆盖 给定一个字符串source和一个目标字符串target,在字符串s ...
- 【打印代码+好好理解+子串问题】LeetCode 76. Minimum Window Substring
LeetCode 76. Minimum Window Substring 字符串子串问题!!!理解这题真是花了很大的功夫!!! 参考链接:https://blog.csdn.net/weixin_4 ...
- Minimum Window Substring @LeetCode
不好做的一道题,发现String Algorithm可以出很多很难的题,特别是多指针,DP,数学推导的题.参考了许多资料: http://leetcode.com/2010/11/finding-mi ...
- LeetCode 76. Minimum Window Substring / 567. Permutation in String
76. Minimum Window Substring 典型Sliding Window的问题,维护一个区间,当区间满足要求则进行比较选择较小的字串,重新修改start位置. 思路虽然不难,但是如何 ...
- [LeetCode] Minimum Window Substring 散列映射问题
题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...
- LeetCode 笔记系列16.3 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]...
题目:Given a string S and a string T, find the minimum window in S which will contain all the characte ...
- LeetCode 笔记系列16.1 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]...
题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...
- LeetCode 笔记系列16.2 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]...
题目:Given a string S and a string T, find the minimum window in S which will contain all the characte ...
- LeetCode - Minimum Window Substring
题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...
最新文章
- css如何做玻璃效果_拓展训练之后的效果保持工作该如何做?
- 批处理-取年月日、时分秒毫秒
- 日志文件记录的重要性
- java springmvc权限校验_详解Spring MVC使用Filter实现登录及权限验证判断
- TF卡里删掉文件后内存没变大_内存卡损坏怎么修复?数据恢复方法教程
- 【三】版本之间穿梭切换
- 成为Linux内核高手的四个方法
- Mysql基本语句(个人笔记)
- 学会这一招,轻松将PDF转网页HTML,快来码住
- Xshell5突然连不上虚拟机
- 浅谈GRADS气象绘图软件的使用
- php服务器怎么设置cookie,PHP之COOKIE支持详解
- 2020年十大数字客户体验(CX)软件平台
- 《金融怪杰》读书笔记
- foreign 磁盘阵列_X3650M4 磁盘阵列
- Win10下EasyConnect闪退、右下角无图标解决方案
- 在Windows 7下最大限度地发挥多显示器的魔力
- 《最高人民法院最高人民检察院关于办理非法利用信息网络、帮助信息网络犯罪活动等刑事案件适用法律若干问题的解释》
- vc模拟 tabletpc_TabletPC上的Windows XPSP2 RC2在FireFox 0.9中不起作用? *喘气*
- 巨型Eclaird雕塑进驻谷歌总部