LeetCode()Minimum Window Substring 超时,但觉得很清晰。

it2022-07-03  110

我的超时思路,感觉自己上了一个新的台阶,虽然超时了,但起码是给出了一个方法。

遍历s 一遍即可,两个指针,当找到了一个合格的字串后,start 开始走,直到遇到s[start]在t中

如果不符合,end++,直到遇到s[end]在t中。

class Solution {public: string minWindow(string s, string t) { int start=0,end=t.size()-1; string res; int len=INT_MAX; map<char,int> coll; for(int i=0;i<t.size();i++) { coll[t[i]]++; } while(end<s.size()) { string str=s.substr(start,end-start+1); if(check(str,t)) { if(end-start+1 < len) { len=end-start+1; res=str; } while(++start < end && coll[s[start]] == 0); } else while(++end<s.size() && coll[s[end]] == 0); } return res; } bool check(string str,string t) { map<char,int> coll; for(int i=0;i<str.size();i++) { coll[str[i]]++; } for(i=0;i<t.size();++i) { if(coll[t[i]]==0) return false; else coll[t[i]]--; } return true; }};

  看了几个别人的,越发觉得我这个思路很清晰,可惜超时,先这样吧。

转载于:https://www.cnblogs.com/yanqi110/p/4997776.html


最新回复(0)