您的位置:首页 > 其它

leetcode 76: Minimum Window Substring

2015-08-05 21:03 477 查看
class Solution {
public:
string minWindow(string s, string t) {
int slen=s.length();
int tlen=t.length();
int need[128]={INT_MIN};
for(int i=0;i<tlen;i++)
need[t[i]]=need[t[i]]==INT_MIN?1:need[t[i]]+1;
int start=0,end=0,count=0,min_len=INT_MAX,min_idx=0;
for(;end<slen;end++)
{
if(need[s[end]]>0)//needed by t
{
need[s[end]]--;
count++;
}
else if(need[s[end]]!=INT_MIN)needed but the number is enough
need[s[end]]--;
if(count==tlen)
{
while(need[s[start]]<0)//find the non-redundant start
{
if(need[s[start]]!=INT_MIN)
++need[s[start++]];
else
start++;
}
if(end-start+1<min_len)//update the min
{
min_len=end-start+1;
min_idx=start;
}
need[s[start]]++;
start++;
count--;
}
}
return min_len==INT_MAX?"":s.substr(min_idx,min_len);
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: