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 ...
随机推荐
- [bzoj1051][HAOI2006]受欢迎的牛——强连通分量
题目大意: 给定一个有向图,求能够被其他所有点访问到的点的个数. 题解: 首先,这个题我在洛谷上AC了,但是bzoj上WA,不知道为什么. 说一下解法. 首先,我们进行scc分解,可以知道, 如果一个 ...
- 多目录,多可执行文件的Makfile的编写
1.前言 在目前的工作中,我遇到这样的一个工作情景,可以认为我要开发一个库,这个库的有多个模块,每个模块的.c放到src中,.h放到include中,这应该是个标准做法. drwxr-xr-x. ro ...
- 《Linux命令、编辑器与shell编程》第三版 学习笔记---001
Linux概述 1.具有内核编程接口 2.支持多用户(同时) 3.支持多任务 4.支持安全的分层文件系统 a.标准 b.链接 c.权限 5.shell(命令解释器和编程语言) a.文件名生成(通配符和 ...
- 【原创】CMD命令设置IP地址
问题描述 在实际工作中,尤其是像我们这种BI分析人员,在做项目的时候,时常都需要因客户的不同随时切换不同的网络环境,有时可能需要在公司和客户之间来回的穿梭.交替.问题也就随之而来:每次客户那里都需要设 ...
- UVA Live 6437 Power Plant 最小生成树
题意: 有许多油井和村庄什么的,让你使得这些村庄能连通一个油井就好了.第一行给你一个数字T代表有T组测试数据,第二行有 M , N , K ,M代表包括油井在内的村庄数,N 代表有N个 两两连通的地方 ...
- 在apache2.4.6中配置虚拟主机支持web.py
web.py 是一个简单好用的python web框架. (http://webpy.org/) apache httpd是一款开源配置简单的web容器. (http://apache.org/) 假 ...
- PHP数组输出三种形式 PHP打印数组
PHP数组输出三种形式 PHP打印数组 $bbbb=array("11"=>"aaa","22"=>"bbb&qu ...
- P2P通信标准协议(三)之ICE
在P2P通信标准协议(二)中,介绍了TURN的基本交互流程,在上篇结束部分也有说到,TURN作为STUN 协议的一个拓展,保持了STUN的工具性质,而不作为完整的NAT传输解决方案,只提供穿透NAT的 ...
- IE8下 input标签内padding失效
在做网页兼容时 发现在ie8下的input内用padding失效 为了达到居中文字的效果 使用line-height可以解决问题
- mtd-utils 工具的使用
mtd_debug [root@xmos /root]# mtd_debugusage: mtd_debug info <device> mtd_debug read < ...