leetcode minimum window substring
2013-08-27 23:29
369 查看
class Solution { public: bool findAll(vector<int> to_find,vector<int> has_find){ bool flag=true; for(int i=0;i<256;i++){ if(to_find[T[i]]<=has_find[T[i]]){ flag=flag&&true; } else{ flag=false; break; } } return flag; } string minWindow(string S, string T) { // Start typing your C/C++ solution below // DO NOT write int main() function int lenT=T.length(); int lenS=S.length(); if(lenT==0) return ""; if(lenS==0) return ""; vector<int> to_find(256); vector<int> has_find(256); for(int i=0;i<lenT;i++){ to_find[T[i]]++; } int left=0; int minleft=0; int minLength=INT_MAX; for(int i=0;i<lenS;i++){ if(to_find[S[i]]>0){ has_find[S[i]]++; //cout<<S[i]; if(findAll(to_find,has_find)){//window contains all chars in T,then move left //assert(S[i]=='b'); //cout<<"in"<<endl; //cout<<S[i]<<endl; while(true){//move left //cout<<has_find[S[left]]<<endl; //cout<<to_find[S[left]]<<endl; if(has_find[S[left]]<=to_find[S[left]]&&has_find[S[left]]!=0){//condition is left is redundant break; } //cout<<"in"<<endl; if(has_find[S[left]]!=0) has_find[S[left]]--;// left++; } if(minLength>(i-left+1)){//see if minlength minLength=i-left+1; minleft=left; } //has_find[S[left]]--; //left++; //cout<<S[left]<<endl; } } } if(minLength==INT_MAX){ return ""; } else{ //cout<<minleft<<minLength; return S.substr(minleft,minLength); } } };
相关文章推荐
- Leetcode Minimum Window Substring
- LeetCode Minimum Window Substring
- leetcode 76 Minimum Window Substring/ 209 Minimum Size Subarray Sum
- LeetCode-Minimum Window Substring-最小窗口子串-滑动窗口算法(尺取法)
- LeetCode : Minimum Window Substring
- [leetcode-76]Minimum Window Substring(java)
- Leetcode -- Minimum Window Substring
- LeetCode - Minimum Window Substring
- LeetCode(76) Minimum Window Substring
- Minimum Window Substring @LeetCode
- [LeetCode] Minimum Window Substring
- [LeetCode] Minimum Window Substring
- leetcode minimum window subString
- LeetCode之Minimum Window Substring
- LeetCode Minimum Window Substring
- LeetCode: Minimum Window Substring
- LeetCode Online Judge 题目C# 练习 - Minimum Window Substring
- 【leetcode】Minimum Window Substring
- Leetcode -- Minimum Window Substring
- LeetCode 笔记系列16.1 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]