原题地址

用两个指针分别记录窗口的左右边界,移动指针时忽略那些出现在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. mariadb介绍

    事务(Transaction):组织多个操作为一个整体,要么全部执行,要么全部不执行 “回滚” ,rollback SQL接口:sql语句分析器和优化器 表:为了满足范式设计要求,将一个数据集分拆为多 ...

  2. 通过API函数来控制SQLite数据库增删改查

    person类属性有Intenger id,String name,Intenger  age,相应的构造方法和set get方法. package com.xh.tx.dao; import and ...

  3. 添加常驻Notification

    private static final int NOTIFICATION_ID=250; //用来标示notification,通过notificatinomanager来发布同样标示的notifi ...

  4. Oracle并行事务回滚相关参数及视图

    /******相关参数****/fast_start_parallel_rollback1.取值有3种:false,low,high2.各值含义:false  ---禁用并行回滚功能          ...

  5. SRF之数据访问

    数据访问组件实现实体类和数据表映射.SQL语句配置执行.动态sql语句等功能,ORM方式能实现简单的对象和表的映射(配置类似hibernate),但比较单一(不支持一对多.多对多的情况),下边不做介绍 ...

  6. LC.exe exited with code -1

    昨天从win8.1升级到win10之后, 一切还算顺利, 就是升级时间比较长. 但是快下班的时候 遇到一个问题, 是之前在win8.1上没遇到的, 首先代码win8.1 vs2013 上跑的时候一切正 ...

  7. 在Linux下JDK1.4.2安装报错的解决方法

    JDK1.4.2的安装   Do you agree to the above license terms? [yes or no]   yes   Unpacking...   tail: cann ...

  8. Hi java新特性

    java新特性 1995.5.23 java语言 1996 jdk1.0 250个类在API 主要用在桌面型应用程序1997 jdk1.1 500 图形用户界面编程1998 jdk1.2 2300 J ...

  9. Android之通过向WebService服务器发送XML数据获取相关服务

    原理图如下:        即客户端向WebService服务器通过HTTP协议发送XML数据(内部包含调用的一些方法和相关参数数据),然后WebService服务器给客户端返回一定的XML格式的数据 ...

  10. Timer Design in StatusBar

    Timer in StatusBar we need to show local time in StatusBar. solution: 1. add textblock control 2. bi ...