LeetCode--Minimum Window Substring
2014-08-04 10:26
411 查看
这题关键注意:T可能有重复,所以得统计次数,我之前理解错了,后来参考别人代码才知道的。
思路就是双指针+字符hash,具体见注释
http://blog.csdn.net/jellyyin/article/details/10313827
这个比较清晰,解释的。不需要用map,因为那样就成了O(nlogn)了
思路就是双指针+字符hash,具体见注释
class Solution { public: string minWindow(string S, string T) { int data[260],i,j; memset(data,0,sizeof(data)); for(i=0;i<T.length();++i) data[T[i]]++; int now[260],left,right,minn=1<<29,num=0; memset(now,0,sizeof(now)); for(i=0,j=0;i<S.length();++i) { //这里的意思就是还没找到包含T所有字符的window的时候就增加右指针 if(num<T.length()) { if(now[S[i]]<data[S[i]])num++; now[S[i]]++; } //找到一个窗口 if(num==T.length()) { //先把窗口前面的 while(j<=i&&now[S[j]]-1>=data[S[j]]) { --now[S[j]]; ++j; } if(minn>i-j+1)left=j,right=i,minn=i-j+1; while(j<=i&&num==T.length()) { now[S[j]]--; if(now[S[j]]<data[S[j]])num--; ++j; } } } string temp; if(minn<1<<29)return temp.assign(S,left,right-left+1); else return ""; } };
http://blog.csdn.net/jellyyin/article/details/10313827
这个比较清晰,解释的。不需要用map,因为那样就成了O(nlogn)了
相关文章推荐
- LeetCode--Minimum Window Substring
- LeetCode-Minimum Window Substring-最小窗口子串-滑动窗口算法(尺取法)
- leetCode_Minimum Window Substring
- [LeetCode] Minimum Window Substring
- Leetcode 76-MinimumWindowSubstring 解题报告
- Leetcode -- Minimum Window Substring
- Minimum Window Substring--LeetCode
- LeetCode - Minimum Window Substring
- [LeetCode]Minimum Window Substring
- Minimum Window Substring -- LeetCode
- LeetCode - Minimum Window Substring
- [leetcode]Minimum Window Substring
- LeetCode之Minimum Window Substring
- LeetCode Minimum Window Substring
- leetcode 76:Minimum Window Substring
- leetcode: Minimum Window Substring
- LeetCode076 Minimum Window Substring
- LeetCode-Minimum Window Substring
- leetcode: Minimum Window Substring
- leetcode: Minimum Window Substring