Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).

For example,
S = "ADOBECODEBANC"
T = "ABC"

Minimum window is "BANC".

Note:
If there is no such window in S that covers all characters in T, return the emtpy string "".

If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.

思路:题目中T可能会有重复的字母 我用了各种分类讨论 结果超时

直接看大神的代码吧

class Solution {
public:
string minWindow(string S, string T) {
int m = S.size(), n = T.size();
if (n <= || m < n) return ""; int require[] = {}; //记录每种字母需要多少个 关键点
for (int i = ; i < n; ++i) require[T[i]]++; int count = ;
int minLen = INT_MAX, minIndex = ;
for (int s = , e = ; e < m; ++e) {
require[S[e]]--; //末尾数字的需求量减1
if (require[S[e]] >= ) count++; //如果需求量大于等于0 说明匹配上了新的数字
while (count == n) { //所有字母都被匹配上了
if (e - s + < minLen) { //长度变小了 记录下新的长度 和 起始位置
minLen = e - s + ;
minIndex = s;
}
require[S[s]]++; //起始位置向后移更新需求量
if (require[S[s]] > ) count--;
s++;
}
} if (minLen == INT_MAX) return "";
return S.substr(minIndex, minLen);
}
};

【leetcode】Minimum Window Substring (hard) ★的更多相关文章

  1. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  2. 【leetcode刷题笔记】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练习题】Minimum Window Substring

    找出包含子串的最小窗口 Given a string S and a string T, find the minimum window in S which will contain all the ...

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

  5. 【LeetCode】159. Longest Substring with At Most Two Distinct Characters

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Given a string S, find the length of the long ...

  6. 【LeetCode】Longest Palindromic Substring 解题报告

    DP.KMP什么的都太高大上了.自己想了个朴素的遍历方法. [题目] Given a string S, find the longest palindromic substring in S. Yo ...

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

  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. 基于zepto判断mobile的横竖屏状态

    借用jquery mobile中的代码,删除了一些多余的部分,简单的基于zepto的模块 var CheckOrientation = (function(){ var win = $( window ...

  2. 自执行的匿名函数!function()

    最近有空可以让我静下心来看看各种代码,function与感叹号的频繁出现,让我回想起2个月前我回杭州最后参加团队会议的时候,@西子剑影抛出的一样的问题:如果在function之前加上感叹号 (!) 会 ...

  3. [译]Create a Web API in MVC 6

    原文: http://www.asp.net/vnext/overview/aspnet-vnext/create-a-web-api-with-mvc-6 ASP.NET 5.0的一个目标是合并MV ...

  4. 【AngularJS】—— 7 模块化

    AngularJS有几大特性,比如: 1 MVC 2 模块化 3 指令系统 4 双向数据绑定 那么本篇就来看看AngularJS的模块化. 首先先说一下为什么要实现模块化: 1 增加了模块的可重用性 ...

  5. .NET之委托

    有些.NET中的高级特性,比如:委托! 有一种怎么也搞不懂的赶脚... 博客读了好几篇,代码也动手写了,书中的一些介绍也看了, 各种搜索关于委托的,至今还处于"会用"的阶段. 该怎 ...

  6. Python-Matplotlib安装及简单使用

    在使用NumPy进行学习统计计算时是枯燥的,大量的数据令我们很头疼,所以我们需要把它图形化显示. Matplotlib是一个Python的图形框架,类似于MATLAB和R语言. Matplotlib的 ...

  7. java集合类

    1.Collection和Collections的区别? (1)Collection是一个接口,为集合对象的基本操作提供通用的接口放法. (2)Collections是一个工具类,里面包含各种对集合的 ...

  8. 正则表达式学习笔记(附:Java版示例代码)

    具体学习推荐:正则表达式30分钟入门教程 .         除换行符以外的任意字符\w      word,正常字符,可以当做变量名的,字母.数字.下划线.汉字\s        space,空白符 ...

  9. Windows下MySQL 5.6安装及配置详细图解

    一.安装前的准备 1.下载安装程序包,可到MySQL官方网站http://www.mysql.com/下载,如图1-1: 图1-1 下载后的安装文件如图1-2所示: 图1-2 二.安装 1.双击下载的 ...

  10. @ModelAttribute运用详解

      @ModelAttribute使用详解 1.@ModelAttribute注释方法     例子(1),(2),(3)类似,被@ModelAttribute注释的方法会在此controller每个 ...