【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 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) ★的更多相关文章
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【leetcode刷题笔记】Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- 【LeetCode练习题】Minimum Window Substring
找出包含子串的最小窗口 Given a string S and a string T, find the minimum window in S which will contain all the ...
- [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 ...
- 【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 ...
- 【LeetCode】Longest Palindromic Substring 解题报告
DP.KMP什么的都太高大上了.自己想了个朴素的遍历方法. [题目] Given a string S, find the longest palindromic substring in S. Yo ...
- [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 ...
- [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 ...
- 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 ...
随机推荐
- git checkout -b 的详细讲解
创建分支: $ git branch mybranch 切换分支: $ git checkout mybranch 创建并切换分支: $ git checkout -b mybranch 更新mast ...
- Windows Server 2008 R2 IIS7.5下PHP、MySQL快速环境配置【图】
众所周知,win平台的服务器版本默认是不能运行php的,需要对服务器进行环境配置. 而许多朋友纠结如何配置,在百度上搜索出的教程一大堆,基本步骤复杂,新手配置容易出错. 今天,邹颖峥教大家一种快速配置 ...
- [AngularJS] jQuery时代
抹平浏览器差异的jQuery出现了 jQuery有什么 jQuery使得开发无刷新动态页面(AJAX)或者单页应用(SAP)变得 相当简单. 标准的HTML页面是静态的,被浏览器渲染后就产生了一个DO ...
- asp.net—缓存
1.页面缓存 要实现页面输出缓存,只要将一条 OutputCache 指令添加到页面即可. <%@ OutputCache CacheProfile=" " NoStore= ...
- solrcloud
@Test public void querySolrCloud(){ String zkHost = "127.0.0.1:2181"; String defaultCollec ...
- PHP get_class_methods函数用法
get_class_methods — 返回由类的方法名组成的数组 说明 array get_class_methods ( mixed $class_name ) 返回由 class_name 指定 ...
- jQuery的$.ajax示例
$.ajax({ url: 'index.php?module=products&submod=product_experience_manage&method=ajaxGetSele ...
- codeblocks+Mingw 下配置开源c++单元测试工具 google test
google test 是google的c++开源单元测试工具,chrome的开发团队就是使用它. Code::Blocks 12.11(MinGW 4.7.1) (Windows版)Google T ...
- IntelliJ Idea 修改编码格式
Setting→Editor→File Encodings→设置“Project Encoding”为UTF-8,如图:
- hihoCoder 1303 数论六·模线性方程组
Description 求解模线性方程组, \(m_i\) 不互质. Sol 扩展欧几里得+中国剩余定理. 首先两两合并跟上篇博文一样. 每次通解就是每次增加两个数的最小公倍数,这对取模任意一个数都是 ...