1. 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).
  2.  
  3. For example,
  4. S = "ADOBECODEBANC"
  5. T = "ABC"
  6.  
  7. Minimum window is "BANC".
  8.  
  9. Note:
  10. If there is no such window in S that covers all characters in T, return the emtpy string "".
  11.  
  12. If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.

分析: 双指针,动态维护一个区间。尾指针不断往后扫,当扫到有一个窗口包含了所有T的字符后,然后再收缩头指针,直到不能再收缩为止。最后记录所有可能的情况中窗口最小的

  1. class Solution {
  2. public:
  3. string minWindow(string S, string T) {
  4. // Start typing your C/C++ solution below
  5. // DO NOT write int main() function
  6. vector<int> expect(, );
  7. vector<int> appear(, );
  8. int count = ;
  9. int start = ;
  10. int minstart= ;
  11. int minLen = INT_MAX;
  12.  
  13. for(int i = ; i < T.size(); i++)expect[T[i]]++;
  14.  
  15. for(int i = ; i < S.size() ; i++)
  16. {
  17. if(expect[S[i]] > ){
  18. appear[S[i]]++;
  19. if(appear[S[i]] <= expect[S[i]])
  20. count++;
  21. }
  22. if(count == T.size()){
  23. while(expect[S[start]] == || appear[S[start]] > expect[S[start]]){
  24. appear[S[start]]--;
  25. start++;
  26. }
  27. if(minLen > (i - start +) ){
  28. minLen = i - start + ;
  29. minstart = start;
  30. }
  31. }
  32. }
  33.  
  34. if(minLen == INT_MAX) return string("");
  35.  
  36. return S.substr(minstart, minLen);
  37. }
  38. };

LeetCode_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. leetcode76. Minimum Window Substring

    leetcode76. Minimum Window Substring 题意: 给定字符串S和字符串T,找到S中的最小窗口,其中将包含复杂度O(n)中T中的所有字符. 例如, S ="AD ...

  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. 刷题76. Minimum Window Substring

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

  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. CAD2014启动出现loadlibrary failed with error 87

    系统win8.1 x64 安装AutoCAD2014完成后,启动出现:Loadlibrary failed with error 87:参数错误 重启,重装都没用.查了一晚上,在国外网站上找到解决办法 ...

  2. 2015第29周二AOP

    1.问题:想要添加日志记录.性能监控.安全监测 2.最初解决方案 2.1.最初解决方案:在每个需要的类函数中重复写上面处理代. 缺点:太多重复代码,且紧耦合 2.2.抽象类进行共性设计,子类进行个性设 ...

  3. 14条Yahoo(雅虎)十四条优化原则【转】

    请大家都能好好学习,不要像我一样一扫而过,好好的记下来!不仅仅是晓得一些CSS xhtml就好了,深刻认识到很多的东西需要学习的.很早就用Firebug,但是却没听说过Yslow,这叫不喜欢追求.希望 ...

  4. Android中ListView异步加载数据

    1.主Activity public class MainActivity extends Activity { private ListView listView; private ArrayLis ...

  5. 【剑指offer】面试题24:二叉搜索树的后序遍历序列

    题目: 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 思路: 递归 注意,主要就是假定数组为空时结果为fa ...

  6. 百度系统部 在 北京市海淀区西二旗首创空间大厦 招聘 Python-交付运维系统研发工程师 - 内推网(neitui.Me)

    百度系统部 在 北京市海淀区西二旗首创空间大厦 招聘 Python-交付运维系统研发工程师 - 内推网(neitui.Me) 汪肴肴 (wa**@baidu.com) 发布了 Python-交付运维系 ...

  7. java类的加载顺序

    related URL: http://www.cnblogs.com/guoyuqiangf8/archive/2012/10/31/2748909.html Parent Class: packa ...

  8. 学习 Netty 3.x

    study link: http://netty.io/3.6/guide/#architecture 应用场景: Chat server that requires persistent conne ...

  9. Yii PHP Framework有用新手教程

    说明:由于近期工作工作关系,须要开发一个在Linux下执行的Web Application,须要对如今比較流行的一些PHP框架做一个了解和评估,以下的这篇文章是笔者近期学习一个比較新的PHP Fram ...

  10. Linux驱动设备中的并发控制

    一.基本概念 二.中断屏蔽 三.原子操作 四.自旋锁 五.信号量 六.互斥体 七.自旋锁与信号量的比较 Linux设备驱动中必须解决的一个问题是多个进程对共享资源的并发访问,并发的访问会导致竞态,即使 ...