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 ...
随机推荐
- php手册学习
整型:int 转换为整型:intval(str) 32位最大值64位最大值 不存在整除语法:应用round();四舍五入.integer去除小数. $a = 1234; //十进制数 $a = ...
- android开源代码
Android开源项目--分类汇总 转自:https://github.com/Trinea/android-open-project Android开源项目第一篇——个性化控件(View)篇 包括L ...
- WebBrowser里网页根据文字判断来点击链接 无Name及ID时
uses ActiveX, ComObj, MSHTML; 根据连接文字点击连接- 一般情况下的连接 Procedure HTMLClinkByText(text:string;Wbr:TWebBro ...
- SQL Server 基础:拾遗
1.一条完整的sql语句: select top | distinct 字段, 表达式, 函数, ... from 表表达式 where 筛选条件 group by 分组条件 having 筛选条件 ...
- MVC 区分是哪按键提交FORM
原理: 引用model(@model modelName)的画面,提交到后台的model对象,属性与前台post标签name属性对应来获取值. 前台: @model myModel @using(Ht ...
- ASP.NET中前台调用后台的方法
学习文章:http://www.cnblogs.com/kingteach/archive/2010/11/12/1875633.html 练习代码: 前台: <html xmlns=" ...
- struts2 s:if标签以及 #,%{},%{#}的使用方法等在资料整理
<s:if>判断字符串的问题: 1.判断单个字符:<s:if test="#session.user.username=='c'"> 这样是从session ...
- Android的一些常用命令提示符(cmd)指令
在<Android基础之用Eclipse搭建Android开发环境和创建第一个Android项目>中我曾介绍过如何给Android SDK配置环境变量,现在它就有用武之地了,我们可以直接在 ...
- golang的序列与反序列化
golang写backend之类的应用,还是挺方便的...使用encoding/json包时, 必须注意, 在struct定义的属性必须是exported, 否则不会设置值. 例如:type DRol ...
- 【EF Code First】Migrations数据库迁移
1,打开工具->NuGet程序管理器->程序包管理器控制台 默认项目中要选择 数据访问上下文类 所在的项目 我的DB是在命名空间CodeFirst.UI下的所以选择CodeFirst. ...