Leetcode#76 Minimum Window Substring
用两个指针分别记录窗口的左右边界,移动指针时忽略那些出现在S种但是没有出现在T中的字符
1. 扩展窗口。向右移动右指针,当窗口内的字符即将多于T内的字符时,停止右移
2. 收缩窗口。向右调整左指针,当窗口内的字符即将少于T内的字符时,停止右移
3. 统计结果。如果窗口内的字符包含了T内的所有字符,且窗口更小,更新最小窗口
4. 继续下一个周期(返回1)
大致思路就是这样了,在具体实现中我用了一个left变量记录当前还未完全包含的非重字符数量,比如当前窗口还差2个a和3个b,则left=2,这样,当left=0时就知道已经包含了所有T的字符。
代码:
string minWindow(string S, string T) {
map<char, int> tMap;
map<char, int> windowMap;
int sLen = S.length();
int tLen = T.length();
string window = "";
int left = ;
for (int i = ; i < tLen; i++)
tMap[T[i]]++;
left = tMap.size();
// 扩展
for (int l = , r = ; r < sLen;) {
for (bool shrink = false; r < sLen && !shrink; r++) {
if (tMap.find(S[r]) != tMap.end()) {
windowMap[S[r]]++;
if (windowMap[S[r]] == tMap[S[r]])
left--;
shrink = windowMap[S[r]] >= tMap[S[r]];
}
}
// 收缩
for (; l < r; l++) {
if (tMap.find(S[l]) != tMap.end()) {
if (windowMap[S[l]] <= tMap[S[l]])
break;
windowMap[S[l]]--;
}
}
if (!left && (window.empty() || window.length() > r - l))
window = S.substr(l, r - l);
}
return window;
}
Leetcode#76 Minimum Window Substring的更多相关文章
- [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 解题思路
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 ...
- 刷题76. Minimum Window Substring
一.题目说明 题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n).难度是Hard! 二.我的解答 先说我的思路: ...
- 【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】#76. Minimum Window Substring
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】76. Minimum Window Substring 最小覆盖子串(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 日期 题目地址: https://leet ...
- 76. Minimum Window Substring
题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...
- [Leetcode][JAVA] Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
随机推荐
- 发布阿里云OSS for phpcmsV9整合教程
说明:这个算不上是插件,因为没有安装包,需要手工修改代码. 还有一点就是后台发布文章时上传的附件还是会保存在你的服务器上,基于以下原因: 1.个人的需求是前台页面需要使用thumb函数生成多个缩略图大 ...
- Python多版本安装 Python2.7和Python3.5
声明:本文仅仅在win8.1测试通过! 1.下载 Python2.7,3.5 2.依次安装Python27(c:\Python27) Python35(c:\Python35) 3.c:\Pytho ...
- [转]Openwrt的Inittab
转来一篇关于启动的文章,特意收藏.http://see.sl088.com/wiki/Inittab 文件位于/etc/inittab编辑方法vi /etc/inittab初始内容::sysinit: ...
- 【WPF学习日记】——Window的DataContext绑定ViewModel
1.全局的ViewModel绑定: a)设定全局的ViewModel(App.xaml中): 1 <Application x:Class="MyTest.App" 2 xm ...
- JavaWeb之Servlet:Cookie 和 Session
会话 现实生活中我们会用手机跟对方对话,拿起手机,拨号,然后对面接听,跟着互相通话,最后会话结束. 这个过程也可以用我们的B/S模式来描述: 打开浏览器—>输入地址->发出请求->服 ...
- 行转列求和:不加 in 条件,sum的数据会不会准确?
我的习惯写法,担心不加 in 条件 ,统计结果会包含其他的数据 SELECT ZWKMYE_KJND as 年度,ZWKMYE_KJQJ as 月份,ZWKMYE_DWBH as 单位, ' then ...
- SQLServer异常捕获
在SQLserver数据库中,如果有很多存储过程的时候,我们会使用动态SQL进行存储过程调用存储过程,这时候,很可能在某个环节就出错了,但是出错了我们很难去跟踪到出错的存储过程,此时我们就可以使用异常 ...
- spring中scope(作用越)理解
今天总结了一下spring中作用域scope的用法.在spring中作用域通过配置文件形式的用法如下. <bean id="role" class="spring. ...
- Linux下使用Magent+Memcached缓存服务器集群部署
1.编译安装libevent cd /root/soft_hhf/ wget http://cloud.github.com/downloads/libevent/libevent/libeven ...
- Mysql账号管理
一 用户添加 通过insert 方式添加用户 insert into mysql.user(Host,User,Password) values("localhost"," ...