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 ...
随机推荐
- 软中断网卡处理&Linux高性能外部设备处理机制&SMP
转载:http://blog.csdn.net/freas_1990/article/details/9238183 看了一些linux网卡驱动的处理技术,对有些概念还是无法理解,突然搜到这篇文章,挺 ...
- 我使用的Sublime插件及配置
我使用的Sublime插件及配置 增强型插件 Package Control 快捷键ctrl+~,调出命令行,运行: import urllib.request,os,hashlib; h = '29 ...
- Delphi中获取文件大小
大概有这些方法可以获得文件大小FileSizeByName(需要引用IdGlobal单元)GetFileSizeFileSize(不能获得正在使用的文件大小)FileSeekTFileStream.S ...
- [BZOJ1052][HAOI2007]覆盖问题 二分+贪心
1052: [HAOI2007]覆盖问题 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2053 Solved: 959 [Submit][Sta ...
- AC日记——Little Elephant and Function codeforces 221a
A - Little Elephant and Function 思路: 水题: 代码: #include <cstdio> #include <iostream> using ...
- Redis主从复制、哨兵模式
1.部署主从 环境:主IP:10.0.0.15,端口6379;从IP:10.0.0.16,端口6379. 原理:基于RDB持久化的功能来实现主从复制的功能. a.linux-redis1(10.0.0 ...
- Linux是32位还是64位
命令行输入 file /bin/ls 显示 /sbin/init: ELF 64-bit LSB executable, x86-64 ... 则为64位 file /sbin/init /sbi ...
- 队列 LinkedBlockingQueue
1 api java.util.concurrent包下的新类.LinkedBlockingQueue就是其中之一,是一个阻塞的线程安全的队列,底层采用链表实现. Li ...
- 自己写Tiny6410的Bootloader总结!
1.由于Tiny6410 2G版的Nand flash(K9GAG08U0E)的页大小是8K的,但是s3c6410芯片设置为nand flash启动时先从nand flash复制8K代码到片内内存中去 ...
- 删除VisualStudio 2013中的 "send Feedback" 按钮
在VisualStudio 2013中,在标题栏中增加了一个 "send Feedback" 按钮,用于给微软发送Bug和回馈(或者一个哭脸和笑脸).这个按钮对于开发来说基本上没用 ...