【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 ...
随机推荐
- UESTC_Judgment Day CDOJ 11
Today is the judgment day. The world is ending and all man will pay for their guilt and sin. Now the ...
- LeeCode(Database)-Duplicate Emails
Write a SQL query to find all duplicate emails in a table named Person. +----+---------+ | Id | Emai ...
- hdu 1502 Regular Words_高精度+dp
题意:问按规则排成的串有多少个A(c)>= B(c) >= C(c) 思路:因为写大整数太累,就偷懒了一下直接用java水过 import java.math.BigInteger; im ...
- centos directory server
http://www.aliyun.com/zixun/content/3_12_517262.html CentOS系统安装Directory Server 8.1操作方法 发布时间:2014-12 ...
- Android专项面试训练题(一)
1.下面不可以退出Activity的是?(D) A.finish() B.抛异常强制退出 C.System.exit(0) D.onStop() 解析: A, finish() 方法就是退出activ ...
- Unity 人物跟谁手指的移动(第一种方式)
长夜漫漫无心睡眠,敲敲代码,越敲越来劲! 我发现好多小朋友都在玩熊出没之xxxx这个游戏,居然打了一下午都没玩通第2关,我把测试也叫来陪我一起玩! 结果他也打不通,我再去叫策划,他也没打过,我去叫主管 ...
- 移除UIView上面的所有控件
;i<[view.subviews count];i++){ [ [ view.subviews objectAtindex:i] removeFromsuperview]; }
- 字符串和数字的全排列问题、前i位被i整除问题
// 全排列问题.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> using names ...
- angular在ie8下的一个bug
昨天拿项目在ie8下测试,发现不少bug,其中有一个bug让我很不解,报了一个thead开头的bug,因为已经切回到linux下了,我就不报具体是什么bug了,鼓捣了半天,发现引用angular的应用 ...
- Java——单例设计模式
设计模式:解决某一类问题最行之有效的方法.Java中23种设计模式:单例设计模式:解决一个类在内存中只存在一个对象. 想要保证对象唯一.1,为了避免其他程序过多建立该类对象.先禁止其他程序建立该类对象 ...