https://oj.leetcode.com/problems/minimum-window-substring/

模拟题

这道题细节比较多。从左到右扫一遍模拟着做

  1. class Solution {
  2. public:
  3. string minWindow(string S, string T) {
  4. string ans = "";
  5. if(S.size() < T.size() )
  6. return ans;
  7.  
  8. unordered_map<char,int> count;
  9. unordered_set<char> charInT;
  10. unordered_map<char,int> countT;
  11.  
  12. for(int i = ; i < T.size(); i++)
  13. {
  14. charInT.insert(T[i]);
  15. countT[T[i]]++;
  16. }
  17.  
  18. int ansI = , ansJ = ;
  19. // 先找第一个合法的
  20. for(int i = ; i < S.size(); i++)
  21. {
  22. if(charInT.find(S[i]) != charInT.end())
  23. {
  24. count[S[i]]++;
  25. // 如果都找到了
  26. if(count.size() == countT.size())
  27. {
  28. bool flag = true;
  29. for(unordered_map<char,int>::iterator itr = countT.begin(); itr != countT.end(); itr++)
  30. {
  31. if(itr->second > count[itr->first])
  32. flag = false; // 还没都匹配
  33. }
  34. if(flag)
  35. {
  36. ansJ = i;
  37. ans = S.substr(ansI,ansJ+);
  38. break;
  39. }
  40. }
  41. }
  42. }
  43. // 往后遍历
  44. for(int m = ; m < S.size(); m++)
  45. {
  46. if(charInT.find(S[m]) == charInT.end())
  47. ansI++; // 往前走1个是安全的
  48. else
  49. {
  50. count[S[m]]--;
  51. if(count[S[m]] >= countT[S[m]])
  52. ansI++;
  53. else
  54. {
  55. if(ans.size() > ansJ - m + )
  56. ans = S.substr(m,ansJ - m +);
  57. // find new end
  58. int temp = ansJ;
  59. temp++;
  60. while(temp<S.size() && S[temp] != S[m])
  61. {
  62. if(charInT.find(S[temp]) != charInT.end())
  63. count[S[temp]]++; // 记录新加进来了合法的
  64. temp++;
  65. }
  66. if(temp == S.size()) // 到了最后也没找到
  67. {
  68. return ans;
  69. }
  70. else
  71. {
  72. ansJ = temp;
  73. count[S[temp]]++;
  74. }
  75. }
  76. }
  77. }
  78. }
  79. };

LeetCode OJ--Minimum Window Substring ***的更多相关文章

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

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

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

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

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

  6. 【leetcode】Minimum Window Substring (hard) ★

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

  7. Leetcode#76 Minimum Window Substring

    原题地址 用两个指针分别记录窗口的左右边界,移动指针时忽略那些出现在S种但是没有出现在T中的字符 1. 扩展窗口.向右移动右指针,当窗口内的字符即将多于T内的字符时,停止右移 2. 收缩窗口.向右调整 ...

  8. [LeetCode] 727. Minimum Window Subsequence 最小窗口子序列

    Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequenceof  ...

  9. Minimum Window Substring @LeetCode

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

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

随机推荐

  1. bootstrap-列表组

    <div class="container"> <!-- list-group 列表组 给ul添加 list-group-item 列表项 给li添加 --> ...

  2. Java集合---LinkedList源码解析

    一.源码解析1. LinkedList类定义2.LinkedList数据结构原理3.私有属性4.构造方法5.元素添加add()及原理6.删除数据remove()7.数据获取get()8.数据复制clo ...

  3. finder文件目录跳转快捷键

    finder文件目录跳转快捷键 command+shift+G

  4. CSS3 初步学习

    CSS3有一些是与旧版CSS2.1重叠的,有一些是没有浏览器支持的,全学没必要,下面只记录一下有用的. 一.CSS3边框 1.圆角border-radius border-radius:值越大,角越圆 ...

  5. NHibernate系列文章十二:Load/Get方法

    摘要 NHibernate提供两个方法按主键值查找对象:Load/Get. 1. Load/Get方法的区别 Load: Load方法可以对查询进行优化. Load方法实际得到一proxy对象,并不立 ...

  6. maven自动部署到远程tomcat教程

    使用maven的自动部署功能可以很方便的将maven工程自动部署到远程tomcat服务器,节省了大量时间. 本文章适用于tomcat的7.x ,8.x, 9.x版本. 下面是自动部的步骤 1,首先,配 ...

  7. sqlserver 加内置dll的使用内存

  8. nginx环境下配置nagiosQL-关于nagiosql配置文件

    接上文:nginx环境下配置nagios-关于nginx.conf nagiosql文件应该处于conf/domain/目录下 nagiosql配置如下: ;                  gzi ...

  9. ngx.lua中遇到的小问题

    作者: 胡 志伟 分类: ngx_lua, 开发代码 发布时间: 2013-09-26 08:40 ė 6评论关闭 在使用ngx.redirect 到一个新的地址时,错误日志提示: lua entry ...

  10. 详解Oracle DELETE和TRUNCATE 的区别(摘)

    语法delete from aa truncate table aa 区别 1.delete from后面可以写条件,truncate不可以. 2.delete from记录是一条条删的,所删除的每行 ...