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. Cesium 初始化Viewer

    <pre name="code" class="javascript"><script> var viewer = new Cesium ...

  2. Centos 6.4搭建git服务器【转】

    前阵子公司需要,让我搭个Git服务器,把之前用的SVN上代码迁移到git上去,所以就在阿里云主机上搭了一个,记录了下安装过程,留存文档以备查阅.本篇本章只涉及搭建部分的操作,更多git的使用可以参考文 ...

  3. Java多线程之赛跑游戏(含生成exe文件)

    在JavaSE中,多线程是一个重要的内容. 我们要了解多线程的概念,就要先了解进程的概念:要了解进程的概念,就离不开操作系统的概念. 在一台正常运行的电脑中,计算机硬件(如CPU.内存.硬盘.网卡.显 ...

  4. 学习笔记(二) 瓜娃(guava)的API快速熟悉使用

    1,大纲 让我们来熟悉瓜娃,并体验下它的一些API,分成如下几个部分: Introduction Guava Collection API Guava Basic Utilities IO API C ...

  5. Webcollector应用(二)

    先吐槽一句哀家的人品,总在写好代码之后,网站默默的升级,没有一点点防备... 一.加代理 爬取一个网站的时候,爬了不到一半,IP被封了,整个内部局域网的所有电脑都不能访问网站了. public cla ...

  6. Linux下软件的安装与管理

    1.源码安装方式 2.RPM包方式安装 3.yum安装方式 4.二进制软件安装方式 1.源码安装方式 (1)下载.解压Apache源码: mkdir /apache #在根目录下创建一个apache目 ...

  7. python函数库及函数标准库

    一.系统库提供的内部函数 字符函数库: 1)str.islower() :字符串是否全部是小写 2)str.isspace() :字符串是否为空 3)help(str):查询字符串函数库 4)str. ...

  8. C# 中DateTime的各种使用

    获得当前系统时间: DateTime dt = DateTime.Now; Environment.TickCount可以得到“系统启动到现在”的毫秒值 DateTime now = DateTime ...

  9. csv 文件乱码问题

    问题背景: Pandas.DataFrame 数据结构df在调用df.to_csv()方法生成csv文件格式的字符串(调用df.to_csv('test.csv')直接生成文件也有这个问题)作为字符串 ...

  10. php正则匹配以“abc”开头且不能以“xyz”结尾的字符串

    本文介绍下,用php正则区配以"abc"开头的,且不能以"xyz"结尾的字符串的方法,有需要的朋友参考下. 要求:用php正则表达式匹配以“abc”开头,但结尾 ...