LeetCode_Minimum Window Substring
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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<int> expect(, );
vector<int> appear(, );
int count = ;
int start = ;
int minstart= ;
int minLen = INT_MAX; for(int i = ; i < T.size(); i++)expect[T[i]]++; for(int i = ; i < S.size() ; i++)
{
if(expect[S[i]] > ){
appear[S[i]]++;
if(appear[S[i]] <= expect[S[i]])
count++;
}
if(count == T.size()){
while(expect[S[start]] == || appear[S[start]] > expect[S[start]]){
appear[S[start]]--;
start++;
}
if(minLen > (i - start +) ){
minLen = i - start + ;
minstart = start;
}
}
} if(minLen == INT_MAX) return string(""); return S.substr(minstart, minLen);
}
};
LeetCode_Minimum Window Substring的更多相关文章
- 53. Minimum Window Substring
Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...
- Minimum Window Substring @LeetCode
不好做的一道题,发现String Algorithm可以出很多很难的题,特别是多指针,DP,数学推导的题.参考了许多资料: http://leetcode.com/2010/11/finding-mi ...
- 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 ...
- leetcode76. Minimum Window Substring
leetcode76. Minimum Window Substring 题意: 给定字符串S和字符串T,找到S中的最小窗口,其中将包含复杂度O(n)中T中的所有字符. 例如, S ="AD ...
- 【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 ...
- 刷题76. Minimum Window Substring
一.题目说明 题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n).难度是Hard! 二.我的解答 先说我的思路: ...
- [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][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 ...
随机推荐
- 鹿定制|Lu Couture|鹿定制·高级西装礼服私享定制品牌|芙蓉中路明城国际1425
鹿定制|Lu Couture|鹿定制·高级西装礼服私享定制品牌|芙蓉中路明城国际1425 联系我们
- INTELLIJ IDEA集成CHECKSTYLE(转)
转自:http://www.cnblogs.com/kiwi-wang/p/4166410.html 本文中使用intelliJ IDEA版本为14.0.1,其他版本差异不大,可同样安装. 下载安装C ...
- Eclipse Tomcat : Project facet Java version 1.7 is not supported.
在Eclipse打开一个项目,并用Tomcat运行时,报错:Project facet Java version 1.7 is not supported. 大致的截图如下: "项目中的jd ...
- OSI七层以及各层上的协议
各层简介: [1]物理层:主要定义物理设备标准,如网线的接口类型.光纤的接口类型.各种传输介质的传输速率等.它的主要作用是传输比特流(就是由1.0转化为电流强弱来进行传输,到达目的地后在转化为1.0, ...
- SMO(Sequential Minimal Optimization) 伪代码(注释)
Algorithm: Simplified SMO 这个版本是简化版的,并没有采用启发式选择,但是比较容易理解. 输入: C: 调和系数 tol: 容差 (tolerance) max passes: ...
- JAVA 泛型练习
二分查找: public class Q212 { public static void main(String [] args) { Integer []arr = {1,2,3,4,5,6,7,8 ...
- C#(Net)软件开发常用工具汇总,提高你的开发效率
C#(Net)软件开发常用工具汇总,提高你的开发效率 写代码也要读书,爱全栈,更爱生活.每日更新原创IT编程技术及日常实用技术文章. 我们的目标是:玩得转服务器Web开发,搞得懂移动端,电脑客户端更是 ...
- 自定义悬浮按钮:FloatingButton
floating_button_layout.xml <?xml version="1.0" encoding="utf-8"?> <Rela ...
- Lambda表达式 - 浅谈
概述: 只要有委托参数类型的地方,就可以使用 Lambda表达式.在讲述Lambda表达式之前,有必要先简要说明一下 委托中的"匿名方法": using System; using ...
- java中包的应用
Do2.java package mypack; class Do2 { public static void main(String[] args) { packa.Do3 d=new packa. ...