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. 淘宝首页源码藏美女彩蛋(上)(UED新作2013egg)

    今日,偶尔翻看淘宝源码,发现竟有美女形状源码.如下图: 此段代码在console中运行,结果更为惊叹. 亲手尝试的读者已经看到了代码运行的结果.taobao.com的console打印出了UED的招聘 ...

  2. 微信电话本可免费拨打网络电话 通话一分钟约300K流量

    微信电话本新版本于昨日晚间发布,这是一款智能通讯增强软件,通话双方都下载此APP并开通免费通话功能就能使用微信电话本拨打免费网络电话,在对方无法接通情况下还能将音频转向语音信箱,微信电话本目前支持An ...

  3. javascript高级程序设计---document节点

    document节点是文档的根节点,每张网页都有自己的document节点,window.document就是指向这个节点.只要浏览器开始载入文档,这个节点就开始了 对于HTML文档来说,docume ...

  4. android 4种启动模式

    在android里,有4种activity的启动模式,分别为: “standard” (默认) “singleTop” “singleTask” “singleInstance” 它们主要有如下不同: ...

  5. Android 带清除功能的输入框控件ClearEditText,仿IOS的输入框

    转载请注明出处http://blog.csdn.net/xiaanming/article/details/11066685 今天给大家带来一个很实用的小控件ClearEditText,就是在Andr ...

  6. Unity 视频播放杂谈

    http://www.cnblogs.com/zsb517/p/4060814.html 背景:      游戏机中想加入舞蹈元素,最先的想法是开发舞蹈游戏,然后通过动画来表现舞蹈,给用户提供舞蹈教学 ...

  7. Android学习笔记(七)——常见控件

    //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! Android 给我们提供了大量的 UI控件,下面我们简单试试几种常用的控件. 一.TextView 在布局文 ...

  8. 解压版MySQL安装说明

    一.复制my.ini到MySQL解压的目录 例如:E:\MySQL 二.修改my.ini第39~40行 basedir = "E:\\MySQL" datadir = " ...

  9. Laravel 5.1 文档攻略 —— Eloquent: 读取器和修饰器

    date_range 8月前 tag_faces Woody remove_red_eye 1483 chat0 简介 这一章其实很简单,Model的属性不是和数据表的字段一一对应吗? 那么在存储和呈 ...

  10. C++基础知识(1)----文件操作

    参照 小菜鸟上校 的博客 // file operat.cpp : 定义控制台应用程序的入口点. /*上述例子的主要功能是将一个文件的内容复制到另一个文件中, 这个功能主要由一个函数copy来实现.它 ...