您的位置:首页 > 其它

LC76 Minimum Window Substring

2016-03-07 22:38 309 查看
利用哈希表和滑动窗口来做题。一开始窗口内没有包含所有T的字符,扩大窗口直到包含T所有字符为止。然后再将窗口的左端向右移动,直到不能移动为止(再移动的话窗口内就没有所有T的字符了)。然后再移动窗口右端。如此循环。

class Solution {
public:
string minWindow(string s, string t) {
string str="";
if(s==""||t==""||(s.size()<t.size()))
return str;
vector<int> expect(256,0);
vector<int> real(256,0);
for(int i=0;i<t.size();i++)
expect[t[i]]++;
int front=0;
int end=0;
int len=999999999;
int start=0;
for(;end<s.size();end++)
{
if(expect[s[end]]>0)
{
real[s[end]]++;
if(real[s[end]]<=expect[s[end]])
count++;
}
if(count==t.size())
{
while(expect[s[front]]==0||real[s[front]]>expect[s[front]])
{
real[s[front]]--;
front++;
}
if(len>end-front+1)
{
len=end-front+1;
start=front;
}
}
}
return (999999999==len)?"":s.substr(start,len);
}
};


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: