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 ...
随机推荐
- CAD2014启动出现loadlibrary failed with error 87
系统win8.1 x64 安装AutoCAD2014完成后,启动出现:Loadlibrary failed with error 87:参数错误 重启,重装都没用.查了一晚上,在国外网站上找到解决办法 ...
- 2015第29周二AOP
1.问题:想要添加日志记录.性能监控.安全监测 2.最初解决方案 2.1.最初解决方案:在每个需要的类函数中重复写上面处理代. 缺点:太多重复代码,且紧耦合 2.2.抽象类进行共性设计,子类进行个性设 ...
- 14条Yahoo(雅虎)十四条优化原则【转】
请大家都能好好学习,不要像我一样一扫而过,好好的记下来!不仅仅是晓得一些CSS xhtml就好了,深刻认识到很多的东西需要学习的.很早就用Firebug,但是却没听说过Yslow,这叫不喜欢追求.希望 ...
- Android中ListView异步加载数据
1.主Activity public class MainActivity extends Activity { private ListView listView; private ArrayLis ...
- 【剑指offer】面试题24:二叉搜索树的后序遍历序列
题目: 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 思路: 递归 注意,主要就是假定数组为空时结果为fa ...
- 百度系统部 在 北京市海淀区西二旗首创空间大厦 招聘 Python-交付运维系统研发工程师 - 内推网(neitui.Me)
百度系统部 在 北京市海淀区西二旗首创空间大厦 招聘 Python-交付运维系统研发工程师 - 内推网(neitui.Me) 汪肴肴 (wa**@baidu.com) 发布了 Python-交付运维系 ...
- java类的加载顺序
related URL: http://www.cnblogs.com/guoyuqiangf8/archive/2012/10/31/2748909.html Parent Class: packa ...
- 学习 Netty 3.x
study link: http://netty.io/3.6/guide/#architecture 应用场景: Chat server that requires persistent conne ...
- Yii PHP Framework有用新手教程
说明:由于近期工作工作关系,须要开发一个在Linux下执行的Web Application,须要对如今比較流行的一些PHP框架做一个了解和评估,以下的这篇文章是笔者近期学习一个比較新的PHP Fram ...
- Linux驱动设备中的并发控制
一.基本概念 二.中断屏蔽 三.原子操作 四.自旋锁 五.信号量 六.互斥体 七.自旋锁与信号量的比较 Linux设备驱动中必须解决的一个问题是多个进程对共享资源的并发访问,并发的访问会导致竞态,即使 ...