LeetCode()Minimum Window Substring 超时,但觉得很清晰。
我的超时思路,感觉自己上了一个新的台阶,虽然超时了,但起码是给出了一个方法。
遍历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;
}
};
看了几个别人的,越发觉得我这个思路很清晰,可惜超时,先这样吧。
LeetCode()Minimum Window Substring 超时,但觉得很清晰。的更多相关文章
- [LeetCode] Minimum Window Substring 最小窗口子串
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- [leetcode]Minimum Window Substring @ Python
原题地址:https://oj.leetcode.com/problems/minimum-window-substring/ 题意: Given a string S and a string T, ...
- [LeetCode] Minimum Window Substring 散列映射问题
题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...
- Leetcode Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- [Leetcode] minimum window substring 最小字符窗口
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- Minimum Window Substring @LeetCode
不好做的一道题,发现String Algorithm可以出很多很难的题,特别是多指针,DP,数学推导的题.参考了许多资料: http://leetcode.com/2010/11/finding-mi ...
- LeetCode解题报告—— Minimum Window Substring && Largest Rectangle in Histogram
1. Minimum Window Substring Given a string S and a string T, find the minimum window in S which will ...
- 【LeetCode】76. Minimum Window Substring
Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...
- 53. Minimum Window Substring
Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...
随机推荐
- 翻译:打造基于Sublime Text 3的全能python开发环境
原文地址:https://realpython.com/blog/python/setting-up-sublime-text-3-for-full-stack-python-development/ ...
- Js综合笔记
-----网页禁止复制---- -----网页禁止复制---- <body> <SCRIPT language=javascript type=text/javascript> ...
- mongoDB 数据导出与导入
一.导出 命令格式:在mongodb/bin目录下 mongoexport -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -c 表名 -f 字段 -q 条件导出 --csv ...
- 19 Using Optimizer Hints
19.1 Overview of Optimizer Hints A hint is an instruction to the optimizer. In a test or development ...
- json对象,数组,字符串总结
关于json对象,数组,字符串的总结 什么是json? JSON(JavaScript Object Notation) 一种轻量级的数据交换格式,JSON采用完全独立于语言的文本格式...(来自百 ...
- Eclipse+Tomcat+MAVEN+SVN项目完整环境搭建
1.JDK的安装 首先下载JDK,这个从sun公司官网可以下载,根据自己的系统选择64位还是32位,安装过程就是next一路到底.安装完成之后当然要配置环境变量了. ————————————————— ...
- spring+mongo
一.程序结构
- 【linux】linux下yum安装后Apache、php、mysql默认安装路径
原文:http://blog.csdn.NET/u010175124/article/details/27322757apache:如果采用RPM包安装,安装路径应在 /etc/httpd目录下apa ...
- HTML 文本格式化<b><big><em><i><small><strong><sub><sup><ins><del>
<b> 标签-粗体 定义和用法: <b>标签规定粗体文本. 提示和注释 注释:根据 HTML5 规范,在没有其他合适标签更合适时,才应该把 <b> 标签作为最后的选 ...
- ITPUB网站的知识索引汇总
1. ITPUB知识索引树 http://www.itpub.net/tree/ http://www.itpub.net/pubtree/index.htm 2. ITPUB知识索引贴——全文索引 ...