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 ...
随机推荐
- op编译信赖的库
Table of known prerequisites and their corresponding packages Here's a table with the package name f ...
- HDOJ 1393 Weird Clock(明白题意就简单了)
Problem Description A weird clock marked from 0 to 59 has only a minute hand. It won't move until a ...
- Findbugs初探-使用idea获取findbugs插件
/* * 整天忙于无限重复的黑盒测试,不论是写脚本,还是手工测,都难以脱离黑&&灰盒测试,所以忽然间想,大学时期学过的所有课程就这样扔掉吗?不好!既然选择了做测试,那么就要一直坚持下去 ...
- java中的浮点(float)运算
一. 关于浮点运算,需要说明的几点: 1. 在java中,进行浮点运算并不会处理例外情况,所以,即使除数为0,也不会有例外被抛出; 2. 当运算结果是溢出(Infinity)时,结果为Infin ...
- 自己写的Ext树,Ext3.4,静态全部加载
var load = function(){ /** * 书籍资料目录 */ var bookIT = new Ext.tree.TreeNode({ text:"IT", lea ...
- Java[3] Java多线程相关资料
Java多线程: http://www.uml.org.cn/j2ee/201509093.asp
- SurfaceView类透明背景设置
将SurfaceView背景设置为透明,主要添加以下几句话就可以了: 在SurfaceView创建后设置一下下面的参数: setZOrderOnTop(true); getHolder().setFo ...
- java jodd轻量级开发框架
http://git.oschina.net/huangyong/jodd_demo/blob/master/jodd-example/src/main/java/jodd/example/servi ...
- [置顶] .net技术类面试、笔试题汇总1
1.简述 private. protected. public. internal 修饰符的访问权限. private : 私有成员, 在类的内部才可以访问. protected : 保护成员,该类内 ...
- Windows下用Caffe跑自己的数据(遥感影像)
1 前言 Caffe对于像我这样的初学者来说是一款非常容易上手的深度学习框架.关于用Caffe跑自己的数据这样的博客已经非常多,感谢前辈们为我们提供的这么好的学习资源.这里我主要结合我所在的行业,说下 ...