【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 ...
随机推荐
- qsort排序算法
七种qsort排序方法 <本文中排序都是采用的从小到大排序> 一.对int类型数组排序 int num[100]; Sample: int cmp ( const void *a , ...
- android JNI调用 execlp函数
execlp()函数 execlp函数简单的来说就是C语言中执行系统命令的函数 execlp()会从PATH 环境变量所指的目录中查找符合参数file 的文件名, ...
- [置顶] Jquery发展
jQuery在2006年1月由美国人JohnResig在纽约的barcamp发布,吸引了来自世界各地的众多JavaScript高手加入,由DaveMethvin率领团队进行开发.是继prototype ...
- html 表格中文字的背景色
- Struts2+Spring集成合并
前边单独总结了Struts2,Spring和Ibaits框架了,那么怎么结合使用呢?这次先来看一下Sturts2和Spring的集成合并.其实挺简单的,就是导入各自的jar包以及连接彼此的jar包,分 ...
- Oracle 取上周一到周末日期的查询语句
-- Oracle 取上周一到周末的sql -- 这样取的是 在一周内第几天,是以周日为开始的 select to_char(to_date('20130906','yyyymmdd'),'d') f ...
- andeoid硬件解码学习 (二)
Finally, I must say, finally, we get low-level media APIs in Android, the Android hardware decoding ...
- android一些常用的代码1(收藏)
以下内容来自多个开源项目的整理和自己的项目积累. 1.拨打电话 public static void call(Context context, String phoneNumber) { conte ...
- 使用xib方式创建UITableViewCell,设置Label自动换行注意事项
自定义的UITableViewCell,使用xib方式创建,想要其中的UILabel换行显示:计算Label的高度,让其自动换行,总是没有效果. 我猜测原因可能在于使用了autolayout布局.只要 ...
- sizeof 和 strlen
1. sizeof 1.1 sizeof是一个独立的运算符,不是函数.sizeof给我们提供有关数据项目所分配的内存的大小.例如: 1 2 cout << sizeof(long) < ...