【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 ...
随机推荐
- JavaEE Tutorials (28) - Duke书店案例研究示例
28.1Duke书店的设计和架构43828.2Duke书店接口439 28.2.1Book Java持久化API实体439 28.2.2Duke书店中使用的企业bean440 28.2.3Duke书店 ...
- 【转】高通平台android 环境配置编译及开发经验总结
原文网址:http://blog.csdn.net/dongwuming/article/details/12784535 1.高通平台android开发总结 1.1 搭建高通平台环境开发环境 在高通 ...
- UESTC_握手 CDOJ 913
握手 Time Limit: 2000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit Status ...
- UESTC_Rain in ACStar 2015 UESTC Training for Data Structures<Problem L>
L - Rain in ACStar Time Limit: 9000/3000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Other ...
- swiftTools
String+Exten.swift // // String+Exten.swift // swiftTest // // Created by napiao on 15/11/27. // Cop ...
- crm使用soap更改下拉框的文本值
//C#代码 //UpdateStateValueRequest updateStateValue = new UpdateStateValueRequest //{ // AttributeL ...
- C++ 实现Trim
一.字符串去空格(没有处理字符串中间的空格) lTrim:除去字符串开头的空格 eg." abc123 " --> "abc123 " ...
- SQL创建/修改数据库、表
--创建表 create table 表(a1 varchar(10),a2 char(2)) --为表添加描述信息 EXECUTE sp_addextendedproperty N'MS_Descr ...
- Sql Server 2012启动存储过程
可以通过如下步骤创建 1.打开show advanced options reconfigure 2.打开scan for startup procs,使得sql server在启动时扫描需要运行的p ...
- C# Cache何时使用及使用方法
Cache 即高速缓存.那么cache是怎么样提高系统性能与运行速度呢?是不是在任何情况下用cache都能提高性能?是不是cache用的越多就越好呢?我在近 期开发的项目中有所体会,写下来当作总结也希 ...