【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.
题目意思:
给定一个字符串S和一个字符串T,不要求T的顺序,从S中找出包含T中字符的长度最小的子串,即最小窗口子串。要求算法时间复杂度为O(n)。
解题思路:
这题的考点是两个指针,一个begin在前,一个end在后,当begin和end都包含了T字符串中字符的时候,计算此时begin和end之间的距离,即为一个窗口子串的长度。当end遍历到S的最后时,得到所有算出来的窗口子串中长度最大的那个,即为答案。因为两个指针都从头到尾遍历了一遍,所以时间复杂度是O(n)。
还有一个小技巧就是,怎么才能知道已经包含了所有的字符呢?
可以像桶排序那样,建立一个很大的数组,以T中的字符(ASCI码)为下标的位置设为1,其他为0.
再建立一个很大的数组,遍历S中字符的时候,遍历到一个字符以那个字符为下标设置一个1,当刚好T那个数组中是1的位置对应S的那个数组相应位置全都是非0的时候,就全部包含了T中的字符拉!
至于那个数组多大,就设置为255,包含unicode字符。
代码如下:
#include <iostream>
#include <vector>
#include <string>
using namespace std; class Solution {
public:
string minWindow(string S, string T) {
vector<int> s_vec(,);
vector<int> t_vec(,);
int lp = -, rp = -, answer = 0x7fffffff, begin = , lack = T.size();
//将T中字符对应位置设置成1
for(int i = ;i < T.size();i++){
t_vec[T[i]]++;
}
for(int end = ; end < S.size(); end++){
//负责移动右边的i,直到包含了所有T中的字符
if(s_vec[S[end]] < t_vec[S[end]])
lack--;
s_vec[S[end]]++; if(lack == ){
//负责移动左边的lb
while( begin < end && s_vec[S[begin]] > t_vec[S[begin]]){
s_vec[S[begin]]--;
begin++;
}
//记录下此时的长度和lb,i的位置
if(answer > end - begin + ){
answer = end - begin + ;
lp = begin;
rp = end;
}
//lb向右移一个
while(lack == ){
s_vec[S[begin]]--;
begin += ;
lack += ;
}
}
}
return lp==-?"":S.substr(lp,rp-lp+);
}
}; int main(){
Solution s;
string S = "ADOBECODEBANC";
string T = "ABC";
string r = s.minWindow(S,T);
cout << r << endl;
getchar();
}
【LeetCode练习题】Minimum Window Substring的更多相关文章
- [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] 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 ...
- [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】Minimum Window Substring (hard) ★
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- Leetcode#76 Minimum Window Substring
原题地址 用两个指针分别记录窗口的左右边界,移动指针时忽略那些出现在S种但是没有出现在T中的字符 1. 扩展窗口.向右移动右指针,当窗口内的字符即将多于T内的字符时,停止右移 2. 收缩窗口.向右调整 ...
- [LeetCode] 727. Minimum Window Subsequence 最小窗口子序列
Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequenceof ...
- 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 ...
随机推荐
- Stones(优先队列)
Stones Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Subm ...
- java模拟get/post提交
1:用jdk连接 String action = "xxxxxxxxxxx": URL url = new URL(action); HttpURLConnection http ...
- 单调队列-hdu-4193-Non-negative Partial Sums
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4193 题目大意: 给n个数,a0,a1,...an,求ai,ai+1,...an,a1,a2,... ...
- 《Qt编程的艺术》——5.1 手动布局
在传统的GUI设计中,每个控件(Widget)都要手动地绑定在窗口之上的一个点上(也就是说,这个控件被指定成了给定GUI元素的父对象),同时还要指定这个控件的高度和宽度.作为所有图形元素的基础类,QW ...
- Python标准库:内置函数classmethod(function)
把类函数当作类的一个方法返回. 类方法第一个參数是指明类,跟类中函数一样,第一个參数是指明类实例. 类方法修饰符採用以下的格式来使用: class C: @classmethod def f(cls, ...
- Hacker(五)----黑客专用通道--->端口
计算机中,端口是计算机与外部进行通信交流的出口.计算机本身携带的物理端口(键盘.鼠标.显示器等输入/输出接口)已经无法满足网络通信的要求,因此TCP/IP协议就引入了一种称为Socket的应用程序接口 ...
- Xshell同时向多个会话发送指令的方法
我们平时使用XSHELL.SecureCRT.putty等ssh连接工具连接到远程主机,每次输入指令都是在单一会话窗口,如果有很多台会话,需要同时输入同样的指令,我们就不用一一输入,浪费时间和精力.可 ...
- ueditor+asp.net异步提交,可以实现了,嘿嘿
之前没用过Ueditor去异步提交,最近项目需要用到这个,就下了个来用,结果可能没仔细去看Ueditor的相关介绍文档,然后自己也郁闷了一下才把它弄出来,现在可以实现异步提交了,松口气,把代码贴出来, ...
- c#之process类相关整理
一.根据进程名获取进程的用户名? 需要添加对 System.Management.dll 的引用 using System.Diagnostics; using System.Manageme ...
- 通过一张简单的图,让你搞懂JS的==运算
== 运算的规则: undefined == null,结果是true.且它俩与所有其他值比较的结果都是false. String == Boolean,需要两个操作数同时转为Number. Stri ...