leetcode76. Minimum Window Substring

题意:

给定字符串S和字符串T,找到S中的最小窗口,其中将包含复杂度O(n)中T中的所有字符。

例如,

S =“ADOBECODEBANC”

T =“ABC”

最小窗口为“BANC”。

注意:

如果S中没有覆盖T中所有字符的窗口,返回空字符串“”。

如果有多个这样的窗口,您可以确保在S中始终只有一个唯一的最小窗口。

思路:

一个right指针遍历s,每次遇到t中的字符,在map中减少一个,同时用一个count做统计,当t中所有字符被遍历的时候,做一次统计,并且将left指针移动,直到count != t.length() ,相当于一个窗口在s字符串上配合map表动态滑动。

ac代码:

C++

class Solution {
public:
string minWindow(string s, string t) {
int map[256] = {0};
bool hash[256] = {false};
int slen = s.length();
int tlen = t.length();
if(!slen || !tlen) return "";
int left,right,res,cnt,minleft;
minleft = cnt = left = right = 0;
res = slen + 1;
for(char ch:t)
{
map[ch]++;
hash[ch] = true;
} while(right < slen)
{
if(hash[s[right++]] && --map[s[right - 1]] >= 0)
cnt++; while(cnt == tlen)
{
if(right - left < res)
{
res = right - left;
minleft = left;
} if(hash[s[left++]] && ++map[s[left - 1]] > 0 )
{
cnt--;
}
}
}
if(res > slen) return "";
return s.substr(minleft, res); }
};

python


leetcode76. Minimum Window Substring的更多相关文章

  1. 53. Minimum Window Substring

    Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...

  2. Minimum Window Substring @LeetCode

    不好做的一道题,发现String Algorithm可以出很多很难的题,特别是多指针,DP,数学推导的题.参考了许多资料: http://leetcode.com/2010/11/finding-mi ...

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

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

  5. 刷题76. Minimum Window Substring

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

  6. [Swift]LeetCode76. 最小覆盖子串 | Minimum Window Substring

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  7. [LeetCode] Minimum Window Substring 最小窗口子串

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

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

  9. Java for LeetCode 076 Minimum Window Substring

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

随机推荐

  1. MGR Switch Muti-Primary to single_primary

    MGR Muti-Primary 切换 single_primary 模式 原因:因为希望做ProxySQL+MGR之间Proxy层的消耗测试,需要把原有的MGR多主改为单主模式. 修改MGRgrou ...

  2. caffe Python API 之中值转换

    # 编写一个函数,将二进制的均值转换为python的均值 def convert_mean(binMean,npyMean): blob = caffe.proto.caffe_pb2.BlobPro ...

  3. 2、gitlab 新建项目

    一.创建项目 1.访问gitlab并登录 http://git.xh.com/ 2.点击 Projects -> Starred projects 每个版本的gitlab不太一样但位置都差不多 ...

  4. 转:mysql日志(Windows下开启Mysql慢查询、通用日志)

    一.Windows下开启Mysql慢查询详解 //show variables like '%quer%';查询是否开启了慢查询!! 第一步:修改my.ini(mysql配置文件)  在my.ini中 ...

  5. 解决uc浏览器不支持vw单位的方法

    插入下段代码,使用rem来代替vw <script type="text/javascript"> (function (doc, win) { var docEl = ...

  6. HDU 1560 DNA sequence(IDA*)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1560 题目大意:给出n个字符串,让你找一个字符串使得这n个字符串都是它的子串,求最小长度. 解题思路: ...

  7. scrapy抓取小说

    用scrapy建立一个project,名字为Spider scrapy startproject Spider 因为之前一直用的是电脑自带的python版本,所以在安装scrapy时,有很多问题,也没 ...

  8. 获取 web 项目的绝对路径

    获取 web 项目的绝对路径 <% String path = request.getContextPath(); String basePath = request.getScheme()+& ...

  9. 洛谷P2507 [SCOI2008]配对 [DP,贪心]

    题目传送门 配对 题目描述 你有 n 个整数Ai和n 个整数Bi.你需要把它们配对,即每个Ai恰好对应一个Bp[i].要求所有配对的整数差的绝对值之和尽量小,但不允许两个相同的数配对.例如A={5,6 ...

  10. 去除win7桌面图标小箭头.bat

    reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Icons" ...