原题地址

用两个指针分别记录窗口的左右边界,移动指针时忽略那些出现在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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. 刷题76. Minimum Window Substring

    一.题目说明 题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n).难度是Hard! 二.我的解答 先说我的思路: ...

  5. 【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 ...

  6. 【一天一道LeetCode】#76. Minimum Window Substring

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  7. 【LeetCode】76. Minimum Window Substring 最小覆盖子串(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 日期 题目地址: https://leet ...

  8. 76. Minimum Window Substring

    题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...

  9. [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 ...

随机推荐

  1. TFS build dotCover StyleCop

    FS2010 – Customizing the Build Details View – Summary View http://blogs.msdn.com/b/jpricket/archive/ ...

  2. Cassandra 有限分页策略

    瀑布式分页 如果你的应用只需要瀑布式的分页,那么,Cassandra可以很好的支持,不过记得要指定好排序顺序. CLUSTERING ORDER BY (add_time DESC); 常见的分页,跳 ...

  3. Azkaban遇到的坑-installation Failed.Error chunking

    在使用azkaban做spark作业调度时,在上传zip包时报installation Failed.Error chunking错误,原来是于我们所编写的应用会上传到 MySQL 存储,过大的zip ...

  4. Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException:

    七月 17, 2014 4:56:01 下午 org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service( ...

  5. 了解Unix进程(2)

    1. 每个进程都有一个名字,crusher 可以得到: # process name puts $PROGRAM_NAME 10.downto(1) do | num | $PROGRAM_NAME ...

  6. js控制div动起来

    代码: <html> <head> <title>让div动的测试</title> <script language="javascri ...

  7. cxGrid使用汇总(一)

    1. 去掉cxGrid中台头的Box 解决:在tableview1的ptionsview的groupbybox=false; 2.统计功能 解决:(1) tableview 1. tableview1 ...

  8. ok6410串口裸机总结

    1.串口角色:(1)数据传输通道(2)控制台 2.通讯参数(1)波特率:衡量传输速率的快慢,每秒钟传输数据的位数(bit)(2)数据位:有效数据(3)起始位:线路空闲的时候是高电平,当检测到低电平认为 ...

  9. 刀哥多线程Barrier异步gcd-08-barrier_async

    Barrier 异步 主要用于在多个异步操作完成之后,统一对非线程安全的对象进行更新 适合于大规模的 I/O 操作 代码演练 准备工作 @interface ViewController () { / ...

  10. SQL Server基本操作积累

    一.基本操作 1.将数据绑定到DataGridVirw控件上显示的数据列标题将会是数据库中的字段名称,可以在使用select语句时使用AS关键字将转化为列名的别名 select name AS 姓名 ...