LeetCode-Minimum Window Substring -- 窗口问题
题目描述
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).
For example,
S ="ADOBECODEBANC"
T ="ABC"
Minimum window is"BANC".
Note:
If there is no such window in S that covers all characters in T, return the emtpy string"".
If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.
找出包含T所有字母的最小窗口。
注意:S=bba t=ba。此时right=2时,left=0,移动left时会++m[b],但是此时窗口内仍旧有一个b。所以在right移动时,m[b]=-1,即只要包含即--,而只有m[b]>=0时,才是有效的,才会++count。同样,在移动left时也是,只有m[b]>0时,才会count--。
class Solution {
public:
string minWindow(string S, string T) {
if(S.length()==)
return "";
map<char, int> m;
for(int i=;i<T.length();i++){
if(m.find(T[i])!=m.end())
m[T[i]]++;
else
m[T[i]]=;
}
int left=,right=,min=S.length()+,minStart=,count=;
for(right=;right<S.length();right++){
if(m.find(S[right])==m.end())
continue;
m[S[right]]--;
if(m[S[right]]>=){
count++;
}
while(count==T.length()){
if((right-left+)<min){
min=right-left+;
minStart=left;
}
if(m.find(S[left])!=m.end()){
m[S[left]]++;
if(m[S[left]]>)
count--;
}
left++;
}
}
string res="";
if(min!=S.length()+){
res=S.substr(minStart,min);
}
return res;
}
};
LeetCode-Minimum Window Substring -- 窗口问题的更多相关文章
- [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] 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 @ Python
原题地址:https://oj.leetcode.com/problems/minimum-window-substring/ 题意: Given a string S and a string T, ...
- [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 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 超时,但觉得很清晰。
我的超时思路,感觉自己上了一个新的台阶,虽然超时了,但起码是给出了一个方法. 遍历s 一遍即可,两个指针,当找到了一个合格的字串后,start 开始走,直到遇到s[start]在t中 如果不符合,en ...
- Minimum Window Substring @LeetCode
不好做的一道题,发现String Algorithm可以出很多很难的题,特别是多指针,DP,数学推导的题.参考了许多资料: http://leetcode.com/2010/11/finding-mi ...
- 【LeetCode】76. Minimum Window Substring
Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...
- LeetCode解题报告—— Minimum Window Substring && Largest Rectangle in Histogram
1. Minimum Window Substring Given a string S and a string T, find the minimum window in S which will ...
- leetcode76. Minimum Window Substring
leetcode76. Minimum Window Substring 题意: 给定字符串S和字符串T,找到S中的最小窗口,其中将包含复杂度O(n)中T中的所有字符. 例如, S ="AD ...
随机推荐
- 培训补坑(day5:最小生成树+负环判断+差分约束)
补坑补坑((╯‵□′)╯︵┻━┻) 内容真的多... 一个一个来吧. 首先是最小生成树. 先讲一下生成树的定义 生成树就是在一张图上选取一些边,使得整个图上所有的点都连通. 那么我们要求的最小生成树有 ...
- python xpath 基本用法
转自:http://www.pythoner.cn/home/blog/python-xpath-basic-usage/ Pyer发现 业界资讯 相册 第7期:Pythoner技术交流沙龙 关于我们 ...
- Xcode5 上64位编译 出错No architectures to compile for
http://blog.csdn.net/chocolateloveme/article/details/16900999 详细错误信息如下: No architectures to compile ...
- VS2013 MFC listcontrol 双击编辑
原文地址:http://blog.csdn.net/xianglifighter/article/details/17592209 最近在拿一些小的项目练习MFC,遇到不少问题,期中之一便是修改列表框 ...
- android.useDeprecatedNdk=true
android.useDeprecatedNdk=true ndk{ moduleName "aa" abiFilter "armeabi-v7a" }
- centos tc 端口限速
#http://www.fx114.net/qa-178-108967.aspx#http://professor.blog.51cto.com/996189/1569481/#http://blog ...
- spark streaming 异常No output streams registered, so nothing to execute
实现spark streaming demo时,代码: public static void main (String[] args) { SparkConf conf = new SparkConf ...
- 【linux高级程序设计】(第十五章)UDP网络编程应用 3
UDP组播通信 组播IP地址: D类IP地址 1110.********** 224.0.0.1 ~ 239.255.255.255 组播MAC地址:低23位,直接对应IP地址, 从右数第24位为 ...
- DB2 触发器的写法及表主键结构的修改
DROP TRIGGER TR_MONTHLYCLOSING; CREATE TRIGGER TR_MONTHLYCLOSING NO CASCADE BEFORE INSERT ON PT_MONT ...
- 在MSSQL中将数字转换成中文
具体代码如下: CREATE FUNCTION [dbo].[fn_NumberToChinese] (@number INT) ) AS BEGIN ); ); ); SET @res = ''; ...